Iteration with for and forEach (v7)

Revision 7 of this benchmark created by KBAKBA on


Setup

Array.prototype.forE = function(fun, thisarg) {
      for (var i = 0; i < this.length; i++) {
        fun.call(thisarg, this[i], i, this);
      }
    };
    
    var ar = [];
    var total = 1000000;
    for (var i = 0; i < total; i++) {
      ar.push(~~ (Math.random() * total));
    }
    
    function iterator(it) {
      count += it;
    }

Teardown


    var ar = [];
  

Test runner

Ready to run.

Testing in
TestOps/sec
for
var count = 0;
for (var i = 0; i < ar.length; i++) {
   count += ar[i];
}
ready
forEach
var count = 0;
ar.forEach(iterator);
ready
custom foreach
var count = 0;
ar.forE(iterator);
ready
for + iterator
var count = 0;
for (var i = 0; i < ar.length; i++) {
  iterator.call(null, ar[i]);
}
ready

Revisions

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