array.length vs. cached

JavaScript performance comparison

Test case created by kazuho

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);
};
</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
not-cached
(function(a) {
  var sum = 0;
  for (var i = 0; i < a.length; ++i) {
    sum += a[i];
  }
  return sum;
})(a);
pending…
cached
(function(a) {
  var sum = 0;
  for (var i = 0, aLen = a.length; i < aLen; ++i) {
    sum += a[i];
  }
  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. Here’s a list of current revisions for this page:

0 comments

Add a comment