for loop research (v42)

Revision 42 of this benchmark created on


Description

Varius for-loop variants: * with length caching * with various counter increment (++i, i++) * while alternative

Setup

var arr = [];
    
    for (var i=0; i<1000; i++) {
      arr.push('arr-value-' + i);
    }
    
    function worker() {
      return(1+1);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
classic for-loop
// async test
for (var i=0; i < arr.length; i++) {
  worker();
}
deferred.resolve();
ready
++i
// async test
for (var i=0; i < arr.length; ++i) {
  worker();
}
deferred.resolve();
ready
caching length in loop
// async test
for (var i=0, len=arr.length; i<len; i++) {
  worker();
}
deferred.resolve();
ready
caching length outside loop
// async test
var len=arr.length;

for (var i=0; i<len; i++) {
  worker();
}
deferred.resolve();
ready
double caching length
// async test
var len=arr.length;

for (var i=0, ilen=len; i<ilen; i++) {
  worker();
}
deferred.resolve();
ready
while alternative i--
// async test
var i = arr.length;
while (i--) {
  worker();
};
deferred.resolve();
ready
while alternative --i
// async test
var i = arr.length;
while (--i) {
  worker();
};
deferred.resolve();
ready

Revisions

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