Factorial – recursive, tail recursive and iterative (v8)

Revision 8 of this benchmark created on


Preparation HTML

<script>
  /* recursive */
  
  function factorial_rec(n) {
   if (n) {
    return n * factorial_rec(n - 1);
   }
   return 1;
  }
  
  /* tail recursive */
  
  function factorial_tailrec(n, res) {
   if (n) {
    return factorial_tailrec(n - 1, n * res);
   }
   return res;
  }
  
  /* iterative */
  
  function factorial_iter(n) {
   var res = n;
   while (--n) {
    res = n * res;
   }
   return res;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
recursive
factorial_rec(50)
ready
tail recursive
factorial_tailrec(50, 1)
ready
iterative
factorial_iter(50)
ready

Revisions

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