# factorial olympics

## JavaScript performance comparison

Revision 2 of this test case created by davsket

## Info

compare imperative vs. recursive factorial vs. continuation-passing vs. tail-recursive vs. tail-recursive-cp

## Preparation code

``<script>//assumes n is a non-negative integerfunction factorial1(n) {  var r = 1;  while (n>1) {    r = r * n--;  }  return r;}//assumes n is a non-negative integerfunction factorial2(n) {  return (n<2) ? 1 : factorial2(n-1) * n;}function factorial3(n,ret) {  if (n == 0)    ret(1) ;  else    factorial3(n-1, function (t0) {     ret(n * t0) }) ;}function factorial4(n) {  return tail_fact(n,1) ;} function tail_fact(n,a) {  if (n == 0)    return a ;  else    return tail_fact(n-1,n*a) ;}function factorial5(n,ret) {  tail_fact2(n,1,ret) ;}  function tail_fact2(n,a,ret) {  if (n == 0)    ret(a) ;  else    tail_fact2(n-1,n*a,ret) ;}</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
imperative 20
``factorial1(20);``
pending…
imperative 170
``factorial1(170);``
pending…
recursive 20
``factorial2(20);``
pending…
recursive 170
``factorial2(170);``
pending…
CP 20
``factorial3(20,function(){});``
pending…
CP 170
``factorial3(170,function(){});``
pending…
tail-factorial 20
``factorial4(20);``
pending…
tail-factorial 170
``factorial4(170);``
pending…
tail-factorial-cp 20
``factorial5(20,function(){});``
pending…
tail-factorial-cp 170
``factorial5(170,function(){});``
pending…

## 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: