Recursion

function factorial(n) { if (n < 0) { // Termination condition to prevent infinite recursion document.write("inifite loop") return; } // Base case if (n === 0) { return 1; } // Recursive case return n * factorial(n -1); }

var t0 = performance.now;

document.write(factorial(2));

var t1 = performance.now;

console.log("factorial took " + (t1 - t0) + " milliseconds.")