Caching Array Length

JavaScript performance comparison

Revision 13 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var theArray = []
    for (var j = 0; j < 2000; j++) {
      theArray[j] = [];
      for (var i = 0; i < 2000; i++) {
        theArray[j][i] = i;
      }
    }
};
</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
With caching
var len = theArray.length;
var a = 0;
for (var j = 0; j < len; j++) {
  var len2 = theArray[j].length;
  for (var i = 0; i < len2; i++) {
    a = theArray[j][i];
  }
}
pending…
Without caching
var a = 0;
for (var j = 0; j < theArray.length; j++) {
  for (var i = 0; i < theArray[j].length; i++) {
    a = theArray[j][i];
  }
}
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