array.length vs. cached (v22)

Revision 22 of this benchmark created by Forest on


Setup

var a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; a = a.concat(a, a, a, a, a, a, a, a);

Test runner

Ready to run.

Testing in
TestOps/sec
not-cached
(function(a) {
  var sum = 0;
  for (var i = 0; i < a.length; ++i) {
    sum += a[i];
  }
  return sum;
})(a);
ready
cached
(function(a) {
  var sum = 0;
  for (var i = 0, aLen = a.length; i < aLen; ++i) {
    sum += a[i];
  }
  return sum;
})(a);
ready
cached, decrement
(function(a) {
  var sum = 0;
  for (var i = a.length; i--;) {
    sum += a[i];
  }
  return sum;
})(a);
ready
cached, decrement 2
(function(a) {
  var sum = 0;
  for (var i = a.length; --i;) {
    sum += a[i];
  }
  return sum;
})(a);
ready
cached, decrement 3
(function(a) {
  var sum = 0, i = a.length;
  while(i--) {
    sum += a[i];
  }
  return sum;
})(a);
ready
cached, decrement 4
(function(a) {
  var sum = 0, i = a.length;
  while(--i >=0) {
    sum += a[i];
  }
  return sum;
})(a);
ready
not-cached while
(function(a) {
  var sum = 0, i=0;
  while(i < a.length) {
    sum += a[i++];
  }
  return sum;
})(a);
ready
cached while
(function(a) {
  var sum = 0, i=0, aLen = a.length;
  while(i < aLen) {
    sum += a[i++];
  }
  return sum;
})(a);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.