While vs For Queue vs Reduce (v24)

Revision 24 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
for(; j<items.length; j++) {
  total += items[j].num;
}
ready
While
i = 0;
while(i < items.length) {
  total += items[i].num;
  ++i;
}
ready
Do
i = 0;
do {
  total += items[i].num;
  ++i;
} while(i < items.length);
ready
Reduce
total = items.reduce(function(i,c) { return c+i.num; }, 0)
ready
ForEach
items.forEach(function(i) { total += i.num })
ready
For (reverted)
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.