Caching Array Length

JavaScript performance comparison

Revision 28 of this test case created by Calvin

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var theArray = [];
    var len = ~~(1000 * Math.random());
    var i = -1;
    while (++i < len) {
      theArray.push(Math.random());
    }
    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
for (var i = 0; i < len; i++) {
  copyOfTheArray[i] = theArray[i];
}
pending…
Without caching
for (var i = 0; i < theArray.length; i++) {
  copyOfTheArray[i] = 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