for vs array-foreach (v19)

Revision 19 of this benchmark created on


Setup

var array = Array(200).join('x').split('');
    
    function callback(value, index, object) {
      return value;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Array#forEach
array.forEach(callback);
ready
for-loop
Array.prototype.forEach = function(fn, ctx) {

  if (this === void 0 || this === null || typeof fn !== "function") throw new TypeError

  var t = Object(this),
    len = t.length >>> 0,
    noCtx = (ctx === void 0 || ctx === null)

    for (var i = 0; i < len; i++) {
      if (i in t) {
        if (noCtx) {
          fn(t[i], i, t)
        } else {
          fn.call(ctx, t[i], i, t)
        }
      }
    }
}

array.forEach(callback);
ready

Revisions

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