custom array forEach (v10)

Revision 10 of this benchmark created on


Setup

var array = ["foo", "bar", "baz", "lorem", "ipsum", "lol", "cat"];
  
  var func = function() {
    2 + 2
  };
  
  Array.prototype.forEach2 = function(a) {
    var l = this.length;
    for (var i = 0; i < l; i++) a(this[i], i)
  };
  
  Array.prototype.forEach3 = function(fun /*, thisArg */ ) {

    "use strict";
  
    if (this === void 0 || this === null)
      throw new TypeError();
  
    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof fun !== "function")
      throw new TypeError();
  
    var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
    for (var i = 0; i < len; i++) {
      if (i in t)
        fun.call(thisArg, t[i], i, t);
    }
  };

Test runner

Ready to run.

Testing in
TestOps/sec
for
(function() {
  for (var i = array.length; i--;) 2 + 2;
})()
ready
foreach
array.forEach(func)
ready
custom foreach
array.forEach2(func)
ready
Custom foreach #2
array.forEach3(func)
ready

Revisions

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