Factorials (v2)

Revision 2 of this benchmark created by Joshua Holbrook on


Preparation HTML

<script>
  var factorialR = function (n) {
      return n ? n*factorialR(n-1) : 1;
  };
  
  var factorialL = function  (n) {
    var result = 1;
    for (; n > 1; n--) {
      result *= n;
    }
    return result;
  }
  
  var factorialL2 = function  (n) {
    var result = 1;
    while (n-- > 1) {
      result *= n;
    }
    return result;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Recursive
var r = factorialR(20);
if (r !== 2432902008176640000) {
  throw new Error('Recursive failed hard');
}
ready
Linear
var r = factorialL(20);
if (r !== 2432902008176640000) {
  throw new Error('Linear failed hard');
}
ready
Linear 2
var r = factorialL2(20);
if (r !== 2432902008176640000) {
  throw new Error('Linear failed hard');
}
ready

Revisions

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

  • Revision 1: published by Maciej Małecki on
  • Revision 2: published by Joshua Holbrook on