Hello, programmers and enthusiastic learner, hope you all having great weekend. So today, I am going to show you some for loops of JavaScript with there execution speed/time. Hope it will minimize your execution time and speed up code with some keen msec.
1.for Loop: A regular for loop , it iterate for each array element.
const cars = ["Tesla" , "Tata" ,"Ford" ,"Land Rover", "Audi" ];
console.time();
for(let i= 0 ; i < cars.length ; i++){
console.log(cars[i]);
}
console.timeEnd();
//console Output
PS D:\Loops> node loops.js
Tesla
Tata
Ford
Land Rover
Audi
default: 31.557ms
2.forEach Loop: A forEach loop ,it execute a given function once for each array element.
const cars = ["Tesla" , "Tata" ,"Ford" ,"Land Rover", "Audi" ];
console.time();
cars.forEach((car) => console.log(car));
console.timeEnd();
//console Output
PS D:\Loops> node loops.js
Tesla
Tata
Ford
Land Rover
Audi
default: 28.306ms
3.for of Loop: A for of loop iterate over value of property.
console.time();
for (car of cars) {
console.log(car);
}
console.timeEnd();
//console Output
PS D:\Loops> node loops.js
Tesla
Tata
Ford
Land Rover
Audi
default: 29.918ms
4.for in loop: A for in loop iterate over name/number of property.
console.time();
for (car in cars) {
console.log(car);
}
console.timeEnd();
//console Output
PS D:\Loops> node loops.js
0
1
2
3
4
default: 28.642ms
conclusion:
Here I have provided an average execution time for loops. So take it in consideration next time. Enjoy the coding...
Top comments (10)
In this case you're actually just benchmarking the console.log and seeing slight variations in the time that takes. It doesn't take anywhere near ~6ms just to run a for loop once! Using my little
benchmarkjs
-powered tool (but you can use any benchmarking library), we can get more precise numbers:Source: gist.github.com/connor4312/07da143...
Ok , I will try it.
I won't repeat what Connor Peet say, which is entirely valid.
If I can suggest something, once you've applied his advices you should also try the
for (let...)
loop with decrements instead of increments.I appreciate your advice and also try for (let...).
Hi Tanuja,
I love to see developers working to understand JavaScript better by trying things like this! For a small dataset, I would recommend using the type of loop that will be most easily understood and maintained over time.
There are some interesting loop optimization techniques, though, that can really increase efficiency on very large datasets. Here is an experiment I built in 2003 to test the efficiency of 3 types of loops. JavaScript engines are so fast these days, you have to crank up the number of iterations to see much difference!
Classical for loop is usually always the fastest one in the house. And there are better ways and things to optimize in a given code
Sadly JSPerf is out of the game, there were a lot of interesting moments (especially in DOM)
I always thought that for of , for in , forEach are the warppers around the regular for loop.
So to me I always understood that they would take extra time to compute.
But results here shows otherwise.
Interesting.
I can understand exception for forEach loop cause it's callback. But couldn't wrap my head around about other ones.
I would have liked to see the result of the map function in relation to all this.
cars.map(car=>{
console.log(car);
}
Ok I will do that for sure.
Superstar! Thank you