While vs For Queue vs Reduce (v47)

Revision 47 of this benchmark created on


Setup

var items = [], total = 0, i = 0, j = 0;
    for(;i<100000;i++) {
      items[i] = { num: Math.ceil( Math.random() * 10 ) };
    }

Test runner

Ready to run.

Testing in
TestOps/sec
For
total = 0;
for(j=0; j<items.length; j++) {
  total += items[j].num;
}
ready
While
j = 0;
total = 0;
while(j < items.length) {
  total += items[j].num;
  ++j;
}
ready
Do
j = 0;
total = 0;
if (items.length > 0)
  do {
    total += items[j].num;
    ++j;
  } while(j < items.length);
ready
Reduce
total = items.reduce(function(c, item) { return c + item.num; }, 0);
ready
ForEach
total = 0;
items.forEach(function(item) { total += item.num });
ready
For (reverted)
total = 0;
for(j = items.length-1; j >= 0; j--) {
  total += items[j].num;
}
ready

Revisions

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