Caching Array Length

JavaScript performance comparison

Revision 53 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = ["", 56, function() {}, 93, "lalala", {}], copy = [], i = 0, l = arr.length;
};

Benchmark.prototype.teardown = function() {
    i = 0;
    copy = [];
};
</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
var += 1
for (i; i < l; i += 1) {

    copy[i] = arr[i];
}
pending…
var++
for (i; i < l; i++) {

    copy[i] = arr[i];
}
pending…
var = var + 1
for (i; i < l; i = i + 1) {

    copy[i] = arr[i];
}
pending…
inline vars def
for (var j = 0; j < arr.length; j += 1) {

    copy[i] = arr[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