for-array-tests

JavaScript performance comparison

Revision 2 of this test case created by Uniquely Common

Info

only the condition specified is the for block

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var test_array = new Array(10000);
    for (var i = 0; i < test_array.length; i++) {
        test_array[i] = Math.random();
    }
};
</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
Simplest
var test;

for (var i = 0; i < test_array.length; i++) {
    test = test_array[i];
}
pending…
Outer length precalc
var test;
var length = test_array.length;

for (var i = 0; i < length; i++) {
    test = test_array[i];
}
pending…
Inner length precalc
var test;

for (var i = 0, length = test_array.length; i < length; i++) {
    test = test_array[i];
}
pending…
increment inside
var test;

for (var i = 0, length = test_array.length; i < length;)  {
    test = test_array[i++];
}
 
pending…
setup outside, increment inside
var test;
var i = 0, length = test_array.length;
for (; i < length; )  {
    test = test_array[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