fibonacci (v2)

Revision 2 of this benchmark created on


Description

Comparison of various fibonnaci functions. (The recursive function is limited to fib(20) because it breaks on my pc for high values - it's an advantage, but I'm not expecting it to reach high scores)

Revision 2: extended the limit to 25 for the recursive function - because other browsers are apparently better at it and make it appear more performant than the iterative function. (If it's still better, just keep in mind that the recursive function has an easier test case than the rest of them)

Setup

number = 0;
    
    function rec(n){
      if(n === 0) { return 0; }
      if(n === 1) { return 1; }
      else { return rec(n-2) + rec(n-1); }
    }
    
    function rec_tail(n) {
      function tc_rec(x,y,c){
        if (c === 0) { return x; } 
        else { return tc_rec(y, x+y, c-1); }
      }
      return tc_rec(0,1,n);
    }
    
    function rec_tail_loga(n){
      function tc_rec(a,b,p,q,c){
        if(c === 0){ return b; }
        if(c % 2 === 0){ return tc_rec(a, b, q*q+p*p, q*(q+2*p), c/2); }
        else { return tc_rec(b*q+a*q+a*p, b*p+a*q, p, q, c-1); }
      }
      return tc_rec(1,0,0,1,n);
    }
    
    function iter(n){
      var x=0, y=1, c=0, t;
      while(c !== n){
        t=x; x=y; y+=t; c++;
      }
      return x;
    }

Teardown


    number = 0;
  

Test runner

Ready to run.

Testing in
TestOps/sec
recursive
rec(number++);
if(number > 25) { number = 0; }
ready
tail recursive
rec_tail(number++);
ready
tail recursive (log)
rec_tail_loga(number++);
ready
iterative
iter(number++);
ready

Revisions

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