admin管理员组

文章数量:1302928

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.

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.

Share Improve this question asked Apr 14, 2016 at 12:40 BeingDevBeingDev 4262 gold badges8 silver badges24 bronze badges 6
  • When I flipped the order of tests, the first one (let) was faster. In approx the same proportion. – ptrk Commented Apr 14, 2016 at 12:50
  • what version of node are you using? – maioman Commented Apr 14, 2016 at 12:54
  • @maioman using v5.10 – BeingDev Commented Apr 14, 2016 at 12:57
  • 2 The problem with tests like that is that it's (at least in principle) possible for the runtime optimization to figure out that the code has no side-effects at all, and simply not do it. Make the loops actually do something (something cheap) and see what difference that makes. – Pointy Commented Apr 14, 2016 at 12:59
  • 2 Well my point is that the difference in overhead may be pletely overshadowed by the cost of the actual real work your code is doing. – Pointy Commented Apr 14, 2016 at 13:02
 |  Show 1 more ment

3 Answers 3

Reset to default 8

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).

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.

i can't tell you more but as mentiont in this video (very good), you need smarter code to test this. https://www.youtube./watch?v=65-RbBwZQdU the piler will to magic stuff with your code and might even ereas the loop if you don't use i and the loop is empty

本文标签: javascriptlet vs var performance in nodejs and chromeStack Overflow