Loops comparation

Another one Node.js loops performance comparation article…

Starting data

Using an array with 90000000 objects, it’s enough for testing purposes.

1
2
3
4
5
6
7
8
let array = [];

const init = () => {
const date = new Date();
array = Array(90000000).fill({foo:'bar',fuzz:new Date()-123})
const end = new Date();
console.log(`init: ${(end-date)/1000}s`)
}

The classic for loop (not caching length)

1
2
3
4
5
6
7
8
9
10
11
const loop_1 = () => {
const date = new Date();

for(let i=0;i<array.length;i++){
array[i].foo = 'rab';
array[i].fuzz += 1;
}

const end = new Date();
console.log(`loop_1: ${(end-date)/1000}s`)
}

The classic for loop (cached length)

1
2
3
4
5
6
7
8
9
10
11
12
13
const loop_2 = () => {
const date = new Date();
let i=0;
const len = array.length;

for(i=0;i<len;i++){
array[i].foo = 'rab';
array[i].fuzz += 1;
}

const end = new Date();
console.log(`loop_2: ${(end-date)/1000}s`)
}

For each loop

1
2
3
4
5
6
7
8
9
10
const loop_3 = () => {
const date = new Date();
array.forEach(item => {
item.foo = 'rab';
item.fuzz += 1;
})

const end = new Date();
console.log(`loop_3: ${(end-date)/1000}s`)
}

While loop

1
2
3
4
5
6
7
8
9
10
const loop_4 = () => {
const date = new Date();
let i = array.length;
while(i--){
array[i].foo = 'rab';
array[i].fuzz += 1;
}
const end = new Date();
console.log(`loop_4: ${(end-date)/1000}s`)
}

For of loop

1
2
3
4
5
6
7
8
9
10
11
const loop_5 = () => {
const date = new Date();

for(const item of array){
item.foo = 'rab';
item.fuzz += 1;
}

const end = new Date();
console.log(`loop_5: ${(end-date)/1000}s`)
}

Do while loop

1
2
3
4
5
6
7
8
9
10
const loop_6 = () => {
const date = new Date();
let i = array.length - 1;
do{
array[i].foo = 'rab';
array[i].fuzz += 1;
}while(i--)
const end = new Date();
console.log(`loop_6: ${(end-date)/1000}s`)
}

Execution times (s)

Node.js 6.14.0 Node.js 8.0.0 Node.js 10.0.0
loop_1 2.293 3.974 0.326
loop_2 1.090 2.896 0.357
loop_3 15.606 1.905 1.115
loop_4 0.873 3.082 0.39
loop_5 5.245 0.268 1.374
loop_6 1.16 2.949 0.396

The above table shows execution times using different Node.js versions (6.14.0, 8.0.0 and 10.0.0). These versions are most common versions currently being used.

As we can see, Node.js 10.0.0 comes with better performance regarding to array treatment. That’s great but it’s not always available due to work environment, customer requirements, etc. Since same loop has different execution time depending on your Node.js version, you should use the one which fits better your version.

Conclusion

Depending on your Node.js version you should use a different loop…

Using Node.js 6.14.0:

  • While loop is the fastest
  • Classic for loop using cached length is the second fastest

Using Node.js 8.0.0:

  • For of loop is the fastest
  • For each loop using cached length is the second fastest

Using Node.js 10.0.0:

  • Classic for loop is the fastest
  • For of loop plus while loop are the second fastest loop.

Each version has its own implementation so take care about it.

Note: This test is using a big big big array, usually you should not use so big arrays…

Hope this article helps you to choose the right loop in your code!