for vs forEach (v184)

Revision 184 of this benchmark created by Konstantin Ivanov on


Description

some, every, pop, reduce

Setup

var i, values = [],
          sum = 0;
      for (i = 0; i < 10; i++) {
       values[i] = i;
      }
      
      function add(val) {
       sum += val;
      }
    
      function rsum(previousSum, currentItem) {
        return previousSum + currentItem;
      }

Test runner

Ready to run.

Testing in
TestOps/sec
forEach
values.forEach(add);
ready
for loop, simple
for (i = 0; i < values.length; i++) {
 add(values[i]);
}
ready
for loop, cached length
var len = values.length;
for (i = 0; i < len; i++) {
 add(values[i]);
}
ready
for loop, reverse
for (i = values.length - 1; i >= 0; i--) {
 add(values[i]);
}
ready
forEach more local
(function() {
 var sum = 0;
 values.forEach(function(val) {
  sum += val;
 });
})();
ready
While Reverse Loop
var i = values.length;
while (i--) {
  sum += values[i];
}
ready
some
values.some(add);
ready
some with anonymous fn
values.some(function(e){
   sum += e
});
ready
every with anonymous
values.every(function(e){
    sum += e;
    return true;
});
ready
pop
//
/*
var vcheck = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for (var i = 0; i < vcheck.length; i++) {
    if (values[i] === vcheck[i]) alert('this is wrong!!!');
}
*/

// if ary no longer needed only

while(values.length) {
    sum += values.pop();
}
ready
for pop in condition
for (var i; i = values.pop();) sum += i
ready
pop + push (save, but reverse ary)
var buff = [], i;

while(values.length) {
    sum += i = values.pop();
    buff.push(i);
}

values = buff;
ready
whlile by 3
// for sum only

var i = values.length;

while (i>=0) {
  sum += values[--i];
  sum += (values[--i] || 0);
  sum += (values[--i] || 0);
}
ready
reduce
sum = values.reduce(rsum);
ready

Revisions

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