reverse().forEach() vs. reversal iteration (v4)

Revision 4 of this benchmark created by V. Klepov on


Preparation HTML

<script>
  var pole = [];
  for(var i = 0; i < 500; i++) {
    pole.push([i, i]);
  }
  Array.prototype.each = function(callback, from, to) {
    var length = this.length;
    if(callback instanceof Function === false ||
       length === 0) return this;
  
    if(from === undefined)
       from  = 0;
    if(to === undefined)
       to  = from < 0 && -from < length?0:length - 1;
    
    if(from < 0)
      from = -from >= length?0:length + from;
    else if(from > length - 1)
      from = length - 1;
  
    if(to < 0)
       to = -to >= length?(from > 0?0:length - 1):length + to;
    else if(to > length - 1)
      to = length - 1;
  
    if(from > to) do {
      if(callback.call(this, this[from], from) === false) return false;
    } while(from-- !== to) else if(from < to) do {
      if(callback.call(this, this[from], from) === false) return false;
    } while(from++ !== to) else if(callback.call(this, this[from], from) === false)
    return false; return this;
  };
</script>

Setup

out = [];

Test runner

Ready to run.

Testing in
TestOps/sec
.reverse().forEach()
pole.reverse().forEach(function(pole) {
  out.push(pole);
});
ready
iteration
pole.each(function(pole) {
  out.push(pole);
}, pole.length - 1, 0);
ready
reduceRight w/passing
pole.reduceRight(function(pv, cv) {
  pv.push(cv);
  return pv;
}, pole);
ready
reduceRight (no reduce)
pole.reduceRight(function(pv, cv) {
  pole.push(cv);
});
ready

Revisions

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