Javascript NodeJS 7.9.0 temporary dead zone is decreasing?

I noticed in another question the difference in performance in loops when using let and var declarations.

The initial question correctly answered that using let in a for loop is slower, since let creates a new scope for each iteration to store the let value of the declared variable. More work remains to be done, so it’s normal to be slower. As a reference, I give the code and results in NodeJS (7.9.0) runtime:

Please note that all javascript codes are for NodeJS version 7.9.0

Regular code:

 'use strict'; console.time('var'); for (var i = 0; i < 100000000; i++) {} console.timeEnd('var'); console.time('let'); for (let j = 0; j < 100000000; j++) {} console.timeEnd('let'); 

Output:

 var: 55.792ms let: 247.123ms 

To avoid adding an additional declaration of region j in each iteration of the loop, we declare the variable j immediately before the loop. One would expect that now this should result in the performance of the let loop matching the value of var . BUT BUT !!! This is the code and result for reference:

Code with let defined before the loop:

 'use strict'; console.time('var'); for (var i = 0; i < 100000000; i++) {} console.timeEnd('var'); console.time('let'); let j; for (j = 0; j < 100000000; j++) {} console.timeEnd('let'); 

Output:

 var: 231.249ms let: 233.485ms 

We see that not only the let loop did not become faster, but the var loop became as slow as let alone !!! The only explanation for this is that since we are not in any block or function, both variables are declared in the global scope. However, as indicated here , declaring a let variable in the middle of the scope creates a temporary dead zone. , which leaves the variable j uninitialized, and var initializes the specified variable.

So, running code in a temporary dead zone, although an uninitialized variable is not referenced, should be pretty slow ....

Finally, to show respect, we declare the variable j at the top of the program to show the results of its launch without a temporary dead zone .

Code without temporary dead zone:

 'use strict'; let j; console.time('var'); for (var i = 0; i < 100000000; i++) {} console.timeEnd('var'); console.time('let'); for (j = 0; j < 100000000; j++) {} console.timeEnd('let'); 

Output:

 var: 55.586ms let: 55.009ms 

Now, both let and var loops have similar optimized performance!

Does anyone know if my assumption about the performance of the temporary dead zone is correct, or give another explanation?

+5
source share
1 answer

This is the result of your tests in several versions of Node:

 node-v4.0.0 var: 92ms let: 336ms var: 220ms let: 230ms ===== node-v4.2.2 var: 95ms let: 342ms var: 228ms let: 233ms ===== node-v5.1.0 var: 93.418ms let: 342.050ms var: 264.895ms let: 228.310ms ===== node-v5.12.0 var: 103.254ms let: 340.990ms var: 228.698ms let: 228.213ms ===== node-v6.3.1 var: 109.476ms let: 338.127ms var: 232.381ms let: 241.795ms ===== node-v6.5.0 var: 96.630ms let: 339.570ms var: 686.631ms let: 612.820ms ===== node-v6.7.0 var: 106.760ms let: 349.677ms var: 690.753ms let: 587.444ms ===== node-v7.0.0 var: 95.366ms let: 333.880ms var: 222.668ms let: 234.101ms ===== node-v7.4.0 var: 101.074ms let: 330.778ms var: 221.869ms let: 238.053ms ===== node-v7.8.0 var: 93.604ms let: 338.447ms var: 224.263ms let: 233.313ms ===== node-v7.9.0 var: 92.622ms let: 333.552ms var: 275.980ms let: 230.990ms 

These are not all versions of Node, only the ones I installed locally, so it's easy to test.

Apparently, the behavior is consistent in most versions: adding let outside the loop makes it a little faster, but makes var somewhere else much slower.

Something bad seems to have happened in 6.5.0, but 7.x has been fixed.

It may leave some room for optimization, but I would not worry too much about var more slowly. I would be more interested in let acceleration.

Bringing these examples into functions and the ability to run them several times can cause JIT to enable and optimize functions so that the results may differ from what you see when everything is executed for the first time.

+2
source

Source: https://habr.com/ru/post/1267014/


All Articles