DEV Community 👩‍💻👨‍💻

Cover image for Which JS loop is fast?
Mysterio
Mysterio

Posted on

Which JS loop is fast?

Hello Guys Today i will discuss which loop among "for","while" and "do-while" is faster?
I am comparing only these 3 loops in this post.

Let's get started...

I am going to perform this test at 10 billion value for the loops

For loop -

let count = 0;
let start = new Date().getTime();
for(let i = 0; i<10000000000;i++){
  count += i
}
let end = new Date().getTime();

console.log((end - start)/1000)
Enter fullscreen mode Exit fullscreen mode

OUTPUT -

I performed it 10 times and got the result in seconds

22.01
14.56
14.49
14.93
15.03
15.10
15.30
15.11
16.34
15.92
Enter fullscreen mode Exit fullscreen mode

While loop -

let count = 0;
let start = new Date().getTime();
let i = 0;
while (i < 10000000000){
    count += i;
    i++;
}
let end = new Date().getTime();

console.log((end - start)/1000)
Enter fullscreen mode Exit fullscreen mode

OUTPUT -

I performed it 10 times and got the result in seconds

21.20
14.69
14.42
15.64
16.93
15.79
16.40
16.09
19.05
18.27
Enter fullscreen mode Exit fullscreen mode

do-while loop -

let count = 0;
let start = new Date().getTime();
let i = 0;
do{
    count += i;
    i++
} while (i < 10000000000)
let end = new Date().getTime();

console.log((end - start)/1000)
Enter fullscreen mode Exit fullscreen mode

OUTPUT -

I performed it 10 times and got the result in seconds

22.36
14.83
15.36
14.82
15.48
18.17
22.48
17.08
18.64
18.11
Enter fullscreen mode Exit fullscreen mode

Averages -

  • For loop - 15.87
  • While loop - 17.14
  • Do-while loop - 17.73

From my test results , For loop is slightly better than while and do-while but i am saying this with test results and i can be wrong as well

NOTE - While running the loop first time in every case the time taken is higher compared to other 9 test case, please mention the reason in the comment section if you know that.

THANK YOU FOR CHECKING THIS POST
^^You can help me by some donation at the link below Thank you👇👇 ^^
☕ --> https://www.buymeacoffee.com/waaduheck <--

Also check these posts as well
https://dev.to/shubhamtiwari909/javascript-map-with-filter-2jgo

https://dev.to/shubhamtiwari909/e-quotes-3bng

https://dev.to/shubhamtiwari909/deploy-react-app-on-netlify-kl

Top comments (12)

Collapse
smac89 profile image
smac89

Next time you want to do timings in JS, instead of manually calculating the time, just use console.time(label) and it's companion console.timeEnd(label).

See docs: developer.mozilla.org/en-US/docs/W...

Collapse
shubhamtiwari909 profile image
Mysterio Author

Thank you i will use it

Collapse
rouilj profile image
John P. Rouillard

Which JS interpreter are you using? Is it possible it's running a JIT optimize step as part of the first run that is reflected in the
subsequent runs?

Collapse
shubhamtiwari909 profile image
Mysterio Author

I am using node js for running these

Collapse
rouilj profile image
John P. Rouillard

Ok, so V8. So suspect the answer to your question:

While running the loop first time in every case the time
taken is higher compared to other 9 test case, please
mention the reason in the comment section if you know that.

is probably the JIT compiler/optimizer:

blog.logrocket.com/how-javascript-...

Thread Thread
shubhamtiwari909 profile image
Mysterio Author

Thank i will read this

Collapse
suryakant profile image
Suryakant Patil

Hey, Can you test the below For loop? I guess it will give you slightly better performance.

let i=0;
for(; i<10000000000;i++){
  count += i
}
Enter fullscreen mode Exit fullscreen mode
Collapse
shubhamtiwari909 profile image
Mysterio Author

The code is not correct can you please write it correctly

Collapse
fjones profile image
FJones • Edited on

I'm not sure where you're struggling. The error message is quite telling, in that all that's missing is:

- let i=0;
+ let i=0, count;
Enter fullscreen mode Exit fullscreen mode

Then the snippet runs. Though my quick look over it seems to make it benchmark slower than the for-scoped version, which is a bit surprising.

Thread Thread
shubhamtiwari909 profile image
Mysterio Author

Yeah got it

Collapse
andrewbaisden profile image
Andrew Baisden

Speed and performance really become a serious requirement when you are trying to find the best data structure for your application. Especially when it has a lot of users.

Collapse
shubhamtiwari909 profile image
Mysterio Author

Yes it is and i performed this test because I wanted to see that all these loops are equal or have some sort of difference

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.