Loops (v129)

Revision 129 of this benchmark created by Binyamin on


Description

Array size = 40; Removed some obscure cases which were not faster on any of the platforms

Setup

var arr = [];
    
    for(var i = 0; i < 40; i++) {
        arr[i] = i+1;
    }
    
    // Old browser legacy
    if (!Array.prototype.forEach) {
      Array.prototype.forEach = function(fun) {
        var len = this.length;
        for (var i = 0; i < len; ++i)
            fun(this[i]);
      };
    }

Test runner

Ready to run.

Testing in
TestOps/sec
01) Naive "while" loop + length caching
var i = 0,
    len = arr.length;
while (i < len) {
    arr[i];
    i++;
}
ready
02) Naive "for" loop
var i = 0;
for (; i < arr.length; i++) {
    arr[i];
}
ready
03) Naive "for" loop + length caching
var i = 0,
    len = arr.length;
for (; i < len; i++) {
    arr[i];
}
ready
04) Presave "for" loop
var i = 0;
for (; i < arr.length; ++i) {
    arr[i];
}
ready
05) Presave "for" loop + length caching
var i = 0,
    len = arr.length;
for (; i < len; ++i) {
    arr[i];
}
ready

Revisions

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