Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

let vs var performance in nodejs and chrome

When I test following code in chrome and nodejs, I get following:

Chrome:

for loop with VAR: 24.058ms
for loop with LET: 8.402ms

NodeJS:

for loop with VAR: 4.329ms
for loop with LET: 8.727ms

As per my understanding, because of block scoping LET is faster in chrome. But can someone help me understand why is it opposite in NodeJS? Or am i missing something?

"use strict";
console.time("for loop with VAR");
for (var i = 0; i < 1000000; i += 1) {
 // Do nothing
}
console.timeEnd("for loop with VAR");

console.time("for loop with LET");
for (let i = 0; i < 1000000; i += 1) {
 // Do nothing
}
console.timeEnd("for loop with LET");` 

PS: Not sure if this is not the ideal way to test performance.

like image 570
BeingDev Avatar asked Apr 14 '16 12:04

BeingDev


People also ask

Which is faster let or VAR?

In terms of performance comparison, var is faster and let is slower inside the loops while running or executing the code. Re-declaring var declared a variable in the same function or scope gives rise to Syntax Error whereas let declared variable cannot be redeclared.

Why is var better than let?

let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope.

Do Nodejs and chrome have the same JavaScript interpreter?

Node uses the same JS "engine" that runs chrome. An engine in this case, is a piece of software that compiles, or "translates" your JS code into machine code; or the 0s and 1s your computer can understand.

Is Node JS good for performance?

An increasing number of Node. js users (including tech giants such as LinkedIn or PayPal) are praising Node for its impressive performance. It's good at multitasking, doesn't weight down the server, and works on Chrome V8, the fastest JavaScript engine available.


2 Answers

V8 version shipped with node.js 5.10 don't support the temporal dead zone for let bindings.

Chrome instead is using V8 5.0 that support it...but as the vm is not yet optimized to handle TDZ, is normal that for now it's slower (I remember reading people who assert that replacing var with let made the code about 27% slower).

like image 68
Nicola Bizzoca Avatar answered Oct 20 '22 01:10

Nicola Bizzoca


When you do

for (let i = 0; i < 1000000; i += 1) { }

the i value in each loop cycle is a separate reference, which is useful when using the i value in an asynchronous callback. This is slower, but can be faster than alternatives in this usage case.

When instead you use

let j;
for (j = 0; j < 1000000; ++j) { }

you will only have one value reference, and it will be just as fast as with var.

Try the following code

console.time("let i");
for (let i = 0; i < 10000000; ++i) { }
console.timeEnd("let i");
console.time("let j");
let j;
for (j = 0; j < 10000000; ++j) { }
console.timeEnd("let j");
console.time("var k");
for (var k = 0; k < 10000000; ++k) { }
console.timeEnd("var k");

this will give results like

let i: 91ms
let j: 25ms
var k: 27ms

where clearly let is equally fast to var when used correctly.

Also to see the difference in asynchronous behaviour, try

for (let i = 0; i < 3; ++i) {
    setImmediate(() => { console.log(i) });
}
let j;
for (j = 0; j < 3; ++j) {
    setImmediate(() => { console.log(j) });
}
for (var k = 0; k < 3; ++k) {
    setImmediate(() => { console.log(k) });
}

which will output

0
1
2
3
3
3
3
3
3

as in each cycle of the loop for let i the i value is a unique reference, which is what causes the slight overhead, whereas for the other two loops it's the same reference.

like image 45
Jan Boon Avatar answered Oct 20 '22 03:10

Jan Boon