fibonacci

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    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;
    }
};

Benchmark.prototype.teardown = function() {
    number = 0;
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
recursive
rec(number++);
if(number > 20) { number = 0; }
pending…
tail recursive
rec_tail(number++);
pending…
tail recursive (log)
rec_tail_loga(number++);
pending…
iterative
iter(number++);
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. Here’s a list of current revisions for this page:

0 comments

Add a comment