loop-for-reduce-forEach

JavaScript performance comparison

Test case created by Micha

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

      
<script>
Benchmark.prototype.setup = function() {
  array1 = new Array(100)
  array2 = new Array(100)

};
</script>

Preparation code output

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(var i=0,n=array1.length; i<n; ++i){
   for(var j=0, m=array2.length; j<m; ++j){
      console.log(!!array1[i], !!array2[j]);
   }
}
pending…
forEach
array1.forEach((a1K) => {
    array2.forEach((a2k) => {
        console.log(!!a1K, !!a2k);
    });
});
pending…
reduce
_.reduce(array1, function(memo, num1){ 
  _.reduce(array2, function(memo, num2){
     console.log(num1, num2)
  }, 0);
}, 0);
pending…
_.each + _.reduce
_.each(array1, function(num1){ 
  _.reduce(array2, function(memo, num2){
     console.log(num1, num2)
  }, 0);
}, 0);
pending…

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

0 Comments