While vs For Queue vs Reduce (v43)

Revision 43 of this benchmark created on


Description

These test cases are an embarrassment. If code is "running" 200,000,000 times in a second then it's a no-op. Note that jsperf runs the "setup" code ONCE then the rest of the code N times.

Setup

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

Test runner

Ready to run.

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

Revisions

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