JS Product

JavaScript performance comparison

Test case created by VishalBty

Preparation code

<div>JS reduce vs map vs foreach vs for loop</div>

[Summary](https://www.thepoorcoder.com/js-reduce-vs-map-vs-foreach-vs-for-loop/)
      
<script>
Benchmark.prototype.setup = function() {
  function range(start, stop) {
  	var array =[];
  	for(var i=start;i<stop;i++){
      	array.push(i)
      }
      return array;
  }
  
  var array = range(0,10000);
  var product = 1;

};
</script>

Preparation code output

<div>JS reduce vs map vs foreach vs for loop</div> [Summary](https://www.thepoorcoder.com/js-reduce-vs-map-vs-foreach-vs-for-loop/)

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
Using for loop
for(var i=0; i<array.length;i++){
    product *= array[i]
}

console.log(product);
pending…
Using foreach loop
array.forEach(i => product*=i)
console.log(product);
pending…
Using map function
array.map(i => product*=i)
console.log(product);
pending…
Using reduce method
var product = array.reduce((a,b) => a * b, 1);
pending…

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

0 Comments

JS reduce vs map vs foreach vs for loop
[Summary](https://www.thepoorcoder.com/js-reduce-vs-map-vs-foreach-vs-for-loop/)