Fast Array Foreach (v58)

Revision 58 of this benchmark created on


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

Revisions

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