reduce-vs-reduceRight

JavaScript performance comparison

Test case created by gartz

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const arr = [1,2,3,4,5,6,7,8,9,10];
  
  const sumValues = (a, b) => a + b;
  
  function reduceRight(arr, cb, x) {
    let i = arr.length;
    let r = x;
    if (r === undefined) {
      i--;
      r = arr[i];
    };
    for(i--; i >= 0; i--) {
      r = cb(r, arr[i], i, arr);
    }
    return r;
  }
  
  Array.prototype.shimReduceRight = function(cb, x) {
    return reduceRight(this, cb, x);
  }

};
</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
reduce
const r = arr.reverse().reduce(sumValues);
pending…
reduceRight
const r = arr.reduceRight(sumValues);
pending…
for
let r = 0;
for(let i = arr.length-1; i >= 0; i--) {
  r = sumValues(r, arr[i]);
}
pending…
for of
let r = 0;
for(let i of arr) {
  r = sumValues(r, i);
}
pending…
shim reduceRight
const r = reduceRight(arr, sumValues);
pending…
context shimReduceRight
const r = arr.shimReduceRight(sumValues);
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