custom array forEach (v22)

Revision 22 of this benchmark created by Mike on


Setup

var array = [];
  for (var q = 0; q < 1000; q++) {
    array[q] = q;
  }
  
  var func = function(ele, idx) {
    ele + idx
  };
  
  Array.prototype.forEach2 = function(a) {
    var l = this.length;
    for (var i = 0; i < l; i++) a(this[i], i)
  }
  
  Array.prototype.map2 = function(a) {
    var l = this.length;
    var array = new Array(l),
      i = 0;
    for (; i < l; i++) {
      array[i] = a(this[i], i)
    }
    return array;
  }

Test runner

Ready to run.

Testing in
TestOps/sec
for
for (var i = array.length; i--;) {
  func(array[i], i);
}
ready
foreach
array.forEach(func)
ready
custom foreach
array.forEach2(func)
ready
map
array.map(func)
ready
custom map
array.map2(func)
ready

Revisions

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