Skip to content

Memoization

Memoization is an optimization technique that caches the return value of a function for a given set of arguments, so subsequent calls with the same inputs return the cached result instantly instead of recomputing.

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

useMemo(f, [x])

Redis, HTTP cache

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

  1. Only pure functions

    If f(x) has side effects or depends on external state, memoization returns stale data.

  2. Bound the cache

    An unbounded memo on a long-running server is a memory leak. Use an LRU map.

  3. 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.

  4. 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
1968 · Michie acuña "memo function"
1980s · memoization en programación dinámica
2018 · React introduce `useMemo` y `React.memo`
40 · llamadas de `fib(40)` con memo vs 300M sin memo

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

Etymology

Origin: borrowed

  • memo (noun): from Latin memorandum, "to be remembered"
  • ization (suffix): action or process

Semantic network

MemoizationoptimizaticachingCachedynamic-prpure-functlru-cache

● broader ● narrower ● related

Broader

  • optimization
  • caching

Related

  • Cache
  • dynamic-programming
  • pure-function
  • lru-cache

Sources