Memoization
Memoization uses closures to cache the results of expensive function calls, avoiding repeated computations.
function memoizedFactorial() { const cache = {}; return function factorial(n) { if (n in cache) return cache[n]; if (n <= 1) return 1; cache[n] = n * factorial(n - 1); return cache[n]; };}const fact = memoizedFactorial();console.log(fact(5)); // 120console.log(fact(5)); // from cache