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)
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
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)
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
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)
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
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)
Next time you want to do timings in JS, instead of manually calculating the time, just use
console.time(label)
and it's companionconsole.timeEnd(label)
.See docs: developer.mozilla.org/en-US/docs/W...
Thank you i will use it
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?
I am using node js for running these
Ok, so V8. So suspect the answer to your question:
is probably the JIT compiler/optimizer:
blog.logrocket.com/how-javascript-...
Thank i will read this
Hey, Can you test the below For loop? I guess it will give you slightly better performance.
The code is not correct can you please write it correctly
I'm not sure where you're struggling. The error message is quite telling, in that all that's missing is:
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.Yeah got it
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.
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