factorial olympics

JavaScript performance comparison

Test case created by Angus

Info

compare imperative vs. recursive factorial

Preparation code

<script>
//assumes n is a non-negative integer
function factorial1(n) {
  var r = 1;
  while (n>1) {
    r = r * n--;
  }
  return r;
}

//assumes n is a non-negative integer
function factorial2(n) {
  return (n<2) ? 1 : factorial2(n-1) * n;
}
</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…

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