While vs For Queue vs Reduce

JavaScript performance comparison

Revision 43 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var items = [], total = 0, i = 0, j = 0;
  for(;i<100000;i++) {
    items[i] = 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 CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
For
for(j=0; j<items.length; j++) {
  total += items[j];
}
pending…
While
j = items.length;
while(j) {
  total += items[--j];
}
pending…
Do
j = items.length;
do {
  total += items[--j];
} while(j);
pending…
Reduce
total = items.reduce(function(i,c) { return c+i; }, 0)
pending…
ForEach
items.forEach(function(i) { total += i })
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.

0 Comments