JS Built-ins
FunctionalProgrammingintermediate

Memoization β€” function called once per unique key

πŸ“– Usage Example
let computeCount = 0;
const memoDouble = memoize(x => { computeCount++; return x * 2; });
memoDouble(5); memoDouble(5);
computeCount; // 1 β€” computed only once

The underlying function is called only once per unique input β€” subsequent calls hit the cache.

Verifying Function Calls Are Reduced

Memoization should call the original function only once per unique input. Subsequent calls with the same input use the cache.

Challenge: Verify that a memoized function calls the underlying function only once for each unique argument.

πŸ’‘ Hints (3)
  • β€’ Track function calls with a counter
  • β€’ Memoized calls should not increment counter
  • β€’ Only first call with unique arg increments counter