factorial recursive vs not recursive (v7)

Revision 7 of this benchmark created by RobG on


Preparation HTML

<script>
  /* recursive */
  
  function factorial(n) {
   var result = 1,
       fac = function(n) {
     return result *= n, n--, (n > 1 ? fac(n) : result);
       };
   return fac(n);
  }
  
  /* non recursive */
  
  function factorialnr(n) {
   var r = n;
   while (--n > 1) {
    r *= r != n ? n : 1;
   }
   return r;
  }
  
  /* non recursive longer */
  
  function factorialnrLong(n) {
   var r = n;
   while (--n > 1) {
    if (r != n) {
     r = r * n;
    } else {
     r = r * 1;
    }
   }
   return r;
  }
  
  /* Minimal non recursive */
  
  function fac0(n) {
   var r = n;
   while (--n) {
    r = r * n;
   }
   return r;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
recursive
factorial(50)
ready
non recursive
factorialnr(50)
ready
/* non recursive, longer */
factorialnrLong(50)
ready
/* minimal non recursive */
fac0(50)
ready

Revisions

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