DEV Community

Tanuja
Tanuja

Posted on

Speedup your JavaScript code using this for loop.

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

conclusion:

Here I have provided an average execution time for loops. So take it in consideration next time. Enjoy the coding...

Top comments (10)

Collapse
 
connor4312 profile image
Connor Peet

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:

        132,000,000 ops/sec > for-let (19.3x)
        114,000,000 ops/sec > forEach (16.6x)
         95,000,000 ops/sec > for of (13.8x)
          6,860,000 ops/sec > for in (1x)

  Benches: 4
  Fastest: for-let
  Elapsed: 21.8s
Enter fullscreen mode Exit fullscreen mode

Source: gist.github.com/connor4312/07da143...

Collapse
 
tanujabshelke profile image
Tanuja

Ok , I will try it.

Collapse
 
vidup profile image
Victor Dupuy

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.

Collapse
 
tanujabshelke profile image
Tanuja

I appreciate your advice and also try for (let...).

Collapse
 
segdeha profile image
Andrew Hedges

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!

Collapse
 
quantuumsnot profile image
quantuumsnot

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)

Collapse
 
srikanth597 profile image
srikanth597

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.

Collapse
 
kwekuq profile image
KwekuQ

I would have liked to see the result of the map function in relation to all this.

cars.map(car=>{
console.log(car);
}

Collapse
 
tanujabshelke profile image
Tanuja

Ok I will do that for sure.

Collapse
 
kwekuq profile image
KwekuQ

Superstar! Thank you