Js sum

JavaScript performance comparison

Test case created by VishalBty

Preparation code


      
      <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,100000)
  var sum = 0;

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

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

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

0 Comments