array.length vs. cached

JavaScript performance comparison

Revision 23 of this test case created by yarikos

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var smallArr = lengthenArray(10);
  bigArr = lengthenArray(1000);
  
  function lengthenArray(source, times) {
    var arr = [];
    for (var i = 0; i < times; i++) {
      arr.push(i);
    }
    return arr;
  }
  
  

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
small not-cached
(function (a) {
  var sum = 0;
  for (var i = 0; i < a.length; ++i) {
    sum += a[i];
  }
  return sum;
})(smallArr)
pending…
small cached
(function (a) {
  var sum = 0;
  for (var i = 0, aLen = a.length; i < aLen; ++i) {
    sum += a[i];
  }
  return sum;
})(smallArr)
pending…
big not-cached
(function (a) {
  var sum = 0;
  for (var i = 0; i < a.length; ++i) {
    sum += a[i];
  }
  return sum;
})(bigArr)
pending…
big cached
(function (a) {
  var sum = 0;
  for (var i = 0, aLen = a.length; i < aLen; ++i) {
    sum += a[i];
  }
  return sum;
})(bigArr)
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.

0 Comments