Caching Array Length

JavaScript performance comparison

Revision 30 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var monkey = {
    theArray: ["", 56,
      function() {},
      93, "lalala", {}
    ]};
    var copyOfTheArray = [];
};
</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 = monkey.theArray.length;
for (var i = 0; i < len; i++) {
  copyOfTheArray[i] = monkey.theArray[i];
}
pending…
Without caching
for (var i = 0; i < monkey.theArray.length; i++) {
  copyOfTheArray[i] = monkey.theArray[i];
}
pending…
With cache in for statement
for (var i = 0, len = monkey.theArray.length; i < len; i++) {
  copyOfTheArray[i] = monkey.theArray[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