array.length vs. cached

JavaScript performance comparison

Revision 47 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; a = a.concat(a, a, a, a, a, a, a, a, a, a, a, a, a);
  
  var someglobalstate = {number:0};
  function addone(a){return a+1;}
  function summer (sumso,a){
  return someglobalstate.number =
      (addone(((sumso+a +1) / 2) * 10)) / 0.99;
  }

};
</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
not-cached
(function(a) {
  var sum = 0;
  for (var i = 0; i < a.length; ++i) {
    sum = summer(sum,a[i]);
    someglobalstate.number++;
  }
  return sum;
})(a);
pending…
cached
(function(a) {
  var sum = 0;
  for (var i = 0, aLen = a.length; i < aLen; ++i) {
   sum = summer(sum,a[i]);
    someglobalstate.number++;
  }
  return sum;
})(a);
pending…
while
(function(a) {
  var sum = 0, i = -1;
  while (++i < a.length) {
    sum = summer(sum, a[i]);
    someglobalstate.number++;
  }
  return sum;
})(a);
pending…
while cached
(function(a) {
  var sum = 0, i = -1, max = a.length;
  while (++i < max) {
    sum = summer(sum, a[i]);
    someglobalstate.number++;
  }
  return sum;
})(a);
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