While vs For Queue vs Reduce

JavaScript performance comparison

Revision 24 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var items = [], total = 0, i = 0, j = 0;
    for(;i<100000;i++) {
      items[i] = { num: Math.ceil( Math.random() * 10 ) };
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
For
for(; j<items.length; j++) {
  total += items[j].num;
}
pending…
While
i = 0;
while(i < items.length) {
  total += items[i].num;
  ++i;
}
pending…
Do
i = 0;
do {
  total += items[i].num;
  ++i;
} while(i < items.length);
pending…
Reduce
total = items.reduce(function(i,c) { return c+i.num; }, 0)
pending…
ForEach
items.forEach(function(i) { total += i.num })
pending…
For (reverted)
for(j = items.length-1; j >= 0; j--) {
  total += items[j].num;
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment