for vs array-foreach (v9)

Revision 9 of this benchmark created on


Setup

var array = Array(31).join('x').split('');
    
    function callback(value, index, object) {
      return value;
    }
    
    function customForEach(array, callback, thisArg) {
          var index = -1,
              length = array.length;
        
          while (++index < length) {
            if (callback(array[index], index, array) === false) {
              break;
            }
          }
          return array;
        }

Test runner

Ready to run.

Testing in
TestOps/sec
for-var loop
for (var index in array) {
  callback(array[index], index, array);
}
ready
for index loop
for(var index = 0, len = array.length; index < len; ++index) {
  callback(array[index], index, array);
}
ready
Native foreach
array.forEach(callback);
ready
Custom Foreach
customForEach(array, callback);
ready

Revisions

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