for vs array-foreach (v15)

Revision 15 of this benchmark created on


Setup

var array = Array(31).join('x').split('');
    
    function callback(value, index, object) {
      return value + index;
    }
    
    function customForEach(array, callback, thisArg) {
      var fn = callback,
        index = -1,
        length = array.length;
    
      while (++index < length) {
        if (callback.call(thisArg, array[index], index, array) === false) {
          break;
        }
      }
      return array;
    }
    
    function foreach(arr, cb, thisArg) {
      var i = 0,
        l = arr.length;
      thisArg = thisArg || this;
      for (; i < l; i++) {
        cb.call(thisArg, arr[i], i, arr);
      }
    }

Test runner

Ready to run.

Testing in
TestOps/sec
custom forEach
customForEach(array, callback);
ready
Array#forEach
array.forEach(callback);
ready
foreach
foreach(array, callback);
ready

Revisions

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