array.length vs. cached

JavaScript performance comparison

Revision 24 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;
  }
  
  function cached(a) {
    var count = 0;
    for (var i = 0, length = a.length; i < length; ++i) {
      count++;
    }
    return count;
  }
  
  
  function notCached(a) {
    var count = 0;
    for (var i = 0; i < a.length; ++i) {
      count++;
    }
    return count;
  }

};
</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 functional expression
(function (a) {
  var sum = 0;
  for (var i = 0; i < a.length; ++i) {
    sum++;
  }
  return sum;
})(smallArr)
pending…
small cached functional expression
(function (a) {
  var sum = 0;
  for (var i = 0, aLen = a.length; i < aLen; ++i) {
    sum++;
  }
  return sum;
})(smallArr)
pending…
big not-cached functional expression
(function (a) {
  var sum = 0;
  for (var i = 0; i < a.length; ++i) {
    sum++;
  }
  return sum;
})(bigArr)
pending…
big cached functional expression
(function (a) {
  var sum = 0;
  for (var i = 0, aLen = a.length; i < aLen; ++i) {
    sum++;
  }
  return sum;
})(bigArr)
pending…
small not-cached function call
notCached(smallArr)
pending…
small cached function call
cached(smallArr)
pending…
big not-cached function call
notCached(bigArr)
pending…
big cached function call
cached(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