For loops comparison (v64)

Revision 64 of this benchmark created by Abraham Toriz Cruz on


Description

Fastest for loop ever

Setup

// Populate the base array
      var arr = [];
      var total = 0;
      for (var i = 0; i < 1000; i++) {
        arr[i] = i;
      }
    
      function someFn(i) {
        return i * 3 * 8;
      }

Test runner

Ready to run.

Testing in
TestOps/sec
Array.ForEach
arr.forEach(function(item) {
  total += someFn(item);
})
ready
For
for (var i = 0, len = arr.length; i < len; i++) {
  total += someFn(arr[i]);
}
ready
alternate For
for (var i = 0, item; item = arr[i]; ++i) {
  total += someFn(item);
}
ready
alternate For
var len = arr.length;

for (var i = 0, len; i < len; i++) {
  total += someFn(arr[i]);
}
ready
alternate For
for (var i in arr) {
  total += someFn(arr[i]);
}
ready
Alternate for with lint compliance
for (var i = 0, item=arr[i]; item; item=arr[++i]) {
  total += someFn(item);
}
ready

Revisions

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