let vs var performance

JavaScript performance comparison

Revision 12 of this test case created

Preparation code

<script type="application/javascript;version=1.7">
  "use strict";

  var firstarray = [];
  for (var i = 0; i < 1000; i++) {
    firstarray[i] = Math.floor(Math.random() * 11);
  }
  var secondarray = [];
  for (var i = 0; i < 1000; i++) {
    secondarray[i] = Math.floor(Math.random() * 11);
  }

  function doNoVar()
  {
    var resultarray = [];
    for (var i = 0; i < 1000; i++) {
      resultarray.push(firstarray[i] + secondarray[i] + 5);
    }
  }

  function doVar()
  {
    var resultarray = [];
    for (var i = 0; i < 1000; i++) {
      var firstnumber = firstarray[i];
      var secondnumber = secondarray[i] + 5;
      var total = firstnumber + secondnumber
      resultarray.push(total);
    }
  }

  function doVar2()
  {
    var resultarray = [];
    var firstnumber = 0;
    var secondnumber = 0;
    var total = 0;
    var i;

    for (i = 0; i < 1000; i++) {
      firstnumber = firstarray[i];
      secondnumber = secondarray[i] + 5;
      total = firstnumber + secondnumber
      resultarray.push(total);
    }
  }

  function doLet()
  {
    let resultarray = [];
    for (let i = 0; i < 1000; i++) {
      let firstnumber = firstarray[i];
      let secondnumber = secondarray[i] + 5;
      let total = firstnumber + secondnumber;
      resultarray.push(total);
    }
  }

  function doLet2()
  {
    let resultarray = [];
    let firstnumber = 0;
    let secondnumber = 0;
    let total = 0;
    let i;

    for (i = 0; i < 1000; i++) {
      firstnumber = firstarray[i];
      secondnumber = secondarray[i] + 5;
      total = firstnumber + secondnumber;
      resultarray.push(total);
    }
  }
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
No var
doNoVar();
pending…
Var
doVar();
pending…
Var 2
doVar2();
pending…
Let
doLet();
pending…
Let 2
doLet2();
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment