title: Javascript temporal dead zone NodeJS 7.9.0 performance decrease?
body: <p>I have noticed in an other <a href="https://stackoverflow.com/questions/37792934/why-is-let-slower-than-var-in-a-for-loop-in-nodejs">question</a> the performance difference in loops while using <code>let</code> and <code>var</code> variables declaration.</p>
  
  <p>The initial  question is correctly answered that using <code>let</code> in the <strong>for loop</strong> is slower since <code>let</code> creates a new scope for each iteration to hold the value of <code>let</code> declared variable. More work to be done, so it is normal to be slower. Just as reference, I give the code and the results in NodeJS (7.9.0) execution time:</p>
  
  <p><em>Please note that all javascript code below regards NodeJS version 7.9.0</em></p>
  
  <p><strong>Regular Code:</strong></p>
  
  <pre><code>'use strict';
  console.time('var');
  for (var i = 0; i &lt; 100000000; i++) {}
  console.timeEnd('var');
  
  
  console.time('let');
  for (let j = 0; j &lt; 100000000; j++) {}
  console.timeEnd('let');
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>var: 55.792ms
  let: 247.123ms
  </code></pre>
  
  <p>In order to avoid the extra scope declaration for <code>j</code> in every iteration of the loop, we declare the <code>j</code> variable just before the loop. One would expect that this should now make the <code>let</code> loop performance match the one of <code>var</code>. <strong>BUT NO!!!</strong> This is the code and the result for reference:</p>
  
  <p><strong>Code with <code>let</code> defined before loop:</strong></p>
  
  <pre><code>'use strict';
  console.time('var');
  for (var i = 0; i &lt; 100000000; i++) {}
  console.timeEnd('var');
  
  
  console.time('let');
  let j;
  for (j = 0; j &lt; 100000000; j++) {}
  console.timeEnd('let');
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>var: 231.249ms
  let: 233.485ms
  </code></pre>
  
  <p>We can see that, not only the <code>let</code> loop did not get any faster but also the <code>var</code> loop became as slow as the <code>let</code> one!!! The only explanation for this is that since we are not in any block or function, both variables are declared in the <strong>global</strong> module scope. However as referenced <a href="http://exploringjs.com/es6/ch_variables.html#sec_temporal-dead-zone" rel="nofollow noreferrer">here</a>, the <code>let</code> declaration of the variable in the middle of the scope, creates a <strong>temporal dead zone</strong>, which leaves the variable <code>j</code> uninitialized, while the <code>var</code> initializes the variable as defined.</p>
  
  <p>So running code in a temporal dead zone although the uninitialized variable is not referenced, must be quite slower....</p>
  
  <p>Finally to show the deference, we declare the <code>j</code> variable on top of the program to show the results of running it without the <strong>temporal dead zone</strong>.</p>
  
  <p><strong>Code without temporal dead zone:</strong></p>
  
  <pre><code>'use strict';
  let j;
  console.time('var');
  for (var i = 0; i &lt; 100000000; i++) {}
  console.timeEnd('var');
  
  
  console.time('let');
  for (j = 0; j &lt; 100000000; j++) {}
  console.timeEnd('let');
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>var: 55.586ms
  let: 55.009ms
  </code></pre>
  
  <p><strong>Now both <code>let</code> and <code>var</code> loops have similar optimized performance!</strong></p>
  
  <p>Does anyone know whether my assumption about temporal dead zone performance is correct, or provide a different explanation???</p>