Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a performance difference between 'let' and 'var' in JavaScript

The difference between these two keywords in terms of scoping has already been thoroughly discussed here, but I was wondering if there is any kind of performance difference between the two, and if so, is it negligible, or at what point would it become significant?

like image 459
Nickolai Avatar asked Jan 30 '14 21:01

Nickolai


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.

Is let or VAR better in JavaScript?

As a general rule, you should always declare variables with const, if you realize that the value of the variable needs to change, go back and change it to let. Use let when you know that the value of a variable will change. Use const for every other variable. Do not use var.

Are let and const faster than VAR in JavaScript?

The execution context underlying how the JavaScript interpreter runs the code is basically the same when you use var compared to when you use let and const . That results in the same execution speed.

What is the main difference between LET and VAR in JavaScript?

The main difference between let and var is that scope of a variable defined with let is limited to the block in which it is declared while variable declared with var has the global scope. So we can say that var is rather a keyword which defines a variable globally regardless of block scope.


2 Answers

After testing this on http://jsperf.com, I got the following results: jsperf has been down for a while; see the replacing code below.

To check this, I'll use the following performance test based on this answer, which led me to write this function:

/**   * Finds the performance for a given function   * function fn the function to be executed   * int n the amount of times to repeat   * return array [time for n iterations, average execution frequency (executions per second)]   */  function getPerf(fn, n) {    var t0, t1;    t0 = performance.now();    for (var i = 0; i < n; i++) {      fn(i)    }    t1 = performance.now();    return [parseFloat((t1 - t0).toFixed(3)), parseFloat((repeat * 1000 / (t1 - t0)).toFixed(3))];  }    var repeat = 100000000;  var msg = '';    //-------inside a scope------------  var letperf1 = getPerf(function(i) {    if (true) {      let a = i;    }  }, repeat);  msg += '<code>let</code> inside an if() takes ' + letperf1[0] + ' ms for ' + repeat + ' iterations (' + letperf1[1] + ' per sec).<br>'    var varperf1 = getPerf(function(i) {    if (true) {      var a = i;    }  }, repeat);  msg += '<code>var</code> inside an if() takes ' + varperf1[0] + ' ms for ' + repeat + ' iterations (' + varperf1[1] + ' per sec).<br>'    //-------outside a scope-----------    var letperf2 = getPerf(function(i) {    if (true) {}    let a = i;  }, repeat);  msg += '<code>let</code> outside an if() takes ' + letperf2[0] + ' ms for ' + repeat + ' iterations (' + letperf2[1] + ' per sec).<br>'    var varperf2 = getPerf(function(i) {    if (true) {}    var a = i;  }, repeat);  msg += '<code>var</code> outside an if() takes ' + varperf1[0] + ' ms for ' + repeat + ' iterations (' + varperf1[1] + ' per sec).<br>'    document.getElementById('out').innerHTML = msg
<output id="out" style="font-family: monospace;white-space: pre-wrap;"></output>

After testing this in Chrome and Firefox, this shows that let is faster than var, but only when inside a different scope than the main scope of a function. In the main scope, var and let are roughly identical in performance. In IE11 and MS Edge, let and var are roughly equal in performance in both cases.

Press the big blue button to see for yourself in your favourite browser.

Currently let has support from only newer browsers, but older browsers are still being used relatively much, which would be a reason to generally not use it yet. If you want to use it somewhere where older browsers wouldn't function otherwise, there should be no problem with it.

Edit: revamped answer since jsperf is not working (see revision history for old version).

like image 71
Joeytje50 Avatar answered Sep 22 '22 11:09

Joeytje50


FYI; After Chrome v60, no further regressions have cropped up. var and let are neck and neck, with var only ever winning by less than 1%. Real world scenarios sometimes give var an advantage due to hoisting and re-use, but at that point you're comparing apples to oranges, as let is intended to allow you to avoid that behavior because the semantics are different.

Benchmark. Firefox, IE and Edge like let just fine.

like image 25
TylerY86 Avatar answered Sep 25 '22 11:09

TylerY86