Looping through array (for in, forEach, custom forEach) (v2)

Revision 2 of this benchmark created on


Setup

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    
    function forEach(arr, foo) {
      var i, len = arr.length;
    
      // loop through the object and execute the function with correct scope
      for (i = 0; i < len; i += 1) {
        foo.call(arr[i]);
    
      }
    }

Test runner

Ready to run.

Testing in
TestOps/sec
for in
var res = 0;
for (var id in arr) {
  res += arr[id];
}
ready
for each
var res = 0;
arr.forEach(function(e) {
  res += e;
});
ready
custom for each
var res = 0;
forEach(arr, function(e) {
  res += e;
});
ready
for..loop
var res = 0;
for ( var i=0, n=arr.length; i<n ; ++i ) {
  res += arr[i];
}
ready

Revisions

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