Definition
What it is
Memoization is a specific type of caching applied to the output of a function. When a function is memoized, the wrapper stores (arguments) -> result in a map. On the next call, if the arguments match, the result is returned from the map instead of recomputing. It is common in pure functions where f(x) always yields the same y.
The technique is prevalent in dynamic programming (Fibonacci, factorial), React (via useMemo and React.memo), and anywhere recursive logic would otherwise re-compute the same branches. It is distinct from general caching (which might be based on time, LRU, or external keys) because the key is strictly the function's argument list.
Why it matters
Consider a naïve Fibonacci implementation: fib(n) = fib(n-1) + fib(n-2). Without memoization, fib(40) makes ~300 million calls. With memoization, it makes 40. The algorithm is the same; the cache collapses the redundant subtrees.
Memoization vs caching vs dynamic programming
Aspect | Memoization | Cache | Dynamic Programming |
|---|---|---|---|
Scope | Function calls | External resources | Algorithm design |
Key | Arguments | URL, ID, query | Subproblems |
Eviction | Usually unbounded | LRU, TTL | N/A (precomputed) |
Example |
| Fibonacci table |
The memoized call
graph TD
Call[Call f(x)] --> Check{In cache?}
Check -->|Yes| Return[Return cached]
Check -->|No| Compute[Compute f(x)]
Compute --> Store[Store in cache]
Store --> Return2[Return result]
style Return fill:#E8F5E9
style Return2 fill:#E8F5E9
How to memoize well
Memoization without memory leaks
Only pure functions
If
f(x)has side effects or depends on external state, memoization returns stale data.Bound the cache
An unbounded memo on a long-running server is a memory leak. Use an LRU map.
Reference equality
memoize(f)([1,2])will not hit the cache for[1,2]because arrays are compared by reference. Use a custom key resolver.Invalidations
If the underlying data changes (e.g., user profile updated), the memo must be invalidated manually.
Common failures
Origin
Donald Michie, a British AI researcher, coined the term in his 1968 paper "Memo Functions and Machine Learning". He described a "memo function" that stores its results for later retrieval, inspired by the Lisp memo function and the Latin memorandum.
The memo function... retains a record of the arguments and results of its past calls.
— Donald Michie, *Memo Functions and Machine Learning*, 1968
Evidence
FAQ
- Memoization vs caching?
Memoization is a function-level cache keyed by arguments. General caching is usually external (Redis, CDN) and time-based.
- What is the biggest risk?
Memory leaks. An unbounded memo dict in a long-running process will grow until OOM.
- When to use it?
Pure functions that are expensive to compute and called repeatedly with the same arguments.
- Can it be async?
Yes, by caching the Promise. But if the Promise rejects, you must clear the cache or you will cache the failure.
Connections
- cache
- dynamic-programming
- pure-function
- lru-cache
Michie, D. (1968). Memo Functions and Machine Learning. https://doi.org/10.1038/218019a0
Cormen, T. H. et al. (2009). Introduction to Algorithms, 3rd ed., Chapter 15: Dynamic Programming. https://mitpress.mit.edu/9780262046305/introduction-to-algorithms/
React (2024). useMemo. https://react.dev/reference/react/useMemo
Etymology
Origin: borrowed
memo(noun): from Latin memorandum, "to be remembered"ization(suffix): action or process
Semantic network
● broader ● narrower ● related
Sources
Michie, D. (1968). Memo Functions and Machine Learning. https://doi.org/10.1038/218019a0
React (2024). useMemo. https://react.dev/reference/react/useMemo