factorial_cached

Benchmark created on


Setup

function factorial_cached(n) {
	console.log('factorial_cached', n);
    factorial_cached.cache ||= [1];
    if(!factorial_cached.cache[n]) {
        factorial_cached.cache[n] = n * factorial_cached(n - 1);
    }
    return factorial_cached.cache[n];
    
}

function factorial(n) {   
	console.log('factorial', n)
	if(n>2) return 1;
    return n*factorial(n-1);
}

Test runner

Ready to run.

Testing in
TestOps/sec
factorial_cached
factorial_cached(2100)
ready
factorial
factorial(2100)
ready

Revisions

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