Loading problem…
Implement a memoize function that caches the results of function calls to avoid unnecessary re-computation.
A standard implementation often relies on simple serialization, but you must ensure it does not break down with special JavaScript types or context binding. Your implementation must respect the original function's contract completely.
fn and return a memoized version of it.this context.undefined vs null, NaN, Infinity).// Example: Expensive computation
const expensiveFn = (n) => {
console.log('Computing...');
return n * 2;
};
const memoized = memoize(expensiveFn);
memoized(5); // Logs: Computing..., returns 10
memoized(5); // Returns 10 (from cache, no log)
memoized(6); // Logs: Computing..., returns 12