js loop comparison ktlapps

JavaScript performance comparison

Test case created by Bruce

Preparation code

<script>
var arr = new Array(1000 + 1).join('0').split('');

var foo = {
    "bar":{
        "method":{
            "add":function(n) {
                return ++n;
            }
        }
    }
};
</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
for loop
var len = arr.length,
    i   = 0;
for (fn=foo.bar.method.add; i<len; i++) {
    arr[i] = fn(arr[i]);
}
pending…
while loop
var len = arr.length,
    i   = 0;
while (len[i]) {
    arr[i] = foo.bar.method.add(arr[i]);
    i++;
}
pending…
do while loop
var len = arr.length,
    i   = 0;
do {
    arr[i] = foo.bar.method.add(arr[i]);
    i++
}
while(len[i]);
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment