using memoization in recursive function

Benchmark created by Truong Nguyen on


Test runner

Ready to run.

Testing in
TestOps/sec
using memoization
// memoization

function memFactorial(n) {
  if (!memFactorial.cache) {
    memFactorial.cache = {
      "0": 1,
      "1": 1
    };
  }
  if (!memFactorial.cache.hasOwnProperty(n)) {
    memFactorial.cache[n] = n * memFactorial(n - 1);
  }
  return memFactorial.cache[n];
}
memFactorial(6);
memFactorial(5);
memFactorial(4);
ready
normal
function factorial(n) {
  if (n < 2)
    return 1;
  else {
    return n * factorial(n - 1);
  }
}
factorial(6);
factorial(5);
factorial(4);
ready

Revisions

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

  • Revision 1: published by Truong Nguyen on