recursion-stack

JavaScript performance comparison

Test case created by wejendorp

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const sum = list => (list.length ? list[0] + sum(list.slice(1)) : 0);
  const sumNoSlice = (list, i = 0) => (i < list.length ? list[i] + sum(list, i + 1) : 0);
  
  const boringSum = list => {
    let acc = 0;
    for(let i = 0; i < list.length; i++) {
      acc += list[i];
    }
    return acc;
  };
  
  const tailcallSum = (list, acc = 0) => (list.length ? sum(list.slice(1), list[0] + acc) : acc);
  
  const tailcallSumNoSlice = (list, acc = 0, i = 0) => (i < list.length ? sum(list, list[i] + acc, i + 1) : acc);

};
</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
sum 2k
sum(Array.from(new Array(2000), () => 1))
pending…
sumNoSlice 2k
sumNoSlice(Array.from(new Array(2000), () => 1))
pending…
tailcallSum 2k
tailcallSum(Array.from(new Array(2000), () => 1))
pending…
tailcallSumNoSlice 2k
tailcallSumNoSlice(Array.from(new Array(2000), () => 1))
pending…
boringSum 2k
boringSum(Array.from(new Array(2000), () => 1))
pending…

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

Compare results of other browsers

0 Comments