Glossary termOptimization technique

Memoization

Also known as: Memoisation, Function caching

Optimization technique that caches a function's results for given inputs so that repeated calls with the same arguments return instantly.

Memoization speeds up programs by remembering the results of expensive function calls. The first time a memoized function is called with particular arguments, it computes the result normally and stores it in a lookup table, typically a hash table, keyed by those arguments. On later calls with the same arguments, it returns the stored result instead of recomputing it. The technique is most valuable when a function is called repeatedly with the same inputs, as in naive recursive calculations of Fibonacci numbers, where memoization turns an exponential-time computation into a linear one.

The British artificial intelligence researcher Donald Michie coined the term in a 1968 paper in the journal Nature, deriving it from memorandum, something to be remembered. Memoization is the top-down form of dynamic programming: the program is written as a natural recursive function, and caching ensures each subproblem is solved only once. Many languages make it easy; Python's standard library, for example, offers caching decorators in its functools module. User interface libraries such as React provide memoization hooks to avoid recomputing values or re-rendering components when their inputs have not changed.

Memoization works safely only for pure functions, whose output depends solely on their inputs and which have no side effects. Caching a function that reads the clock, a database or a random number generator returns stale or wrong results. Other costs include memory, since caches can grow without bound unless they evict old entries, and the overhead of hashing arguments, which can exceed the savings for cheap functions. The same idea operates at larger scales in web caches and content delivery networks, where the hard questions are when cached results become invalid and how much to keep.

Key points

  • Memoization caches function results by argument so that repeated calls skip recomputation.
  • Donald Michie coined the term in 1968, deriving it from the word memorandum.
  • It is the top-down form of dynamic programming, combining natural recursion with caching.
  • Only pure functions can be memoized safely, and unbounded caches can consume large amounts of memory.

Related entries

Glossary term

Algorithmic technique that solves complex problems by breaking them into overlapping subproblems and storing each result so it is computed only once.

Glossary term

Recursion

Programming

Technique in which a function solves a problem by calling itself on smaller instances of the same problem until it reaches a simple base case.

Glossary term

Pure Function

Programming

Function that always returns the same output for the same inputs and has no side effects, such as modifying outside state or performing I/O.

Glossary term

Hash Table

Programming

Data structure that stores key-value pairs and uses a hash function to compute where each item lives, giving very fast average lookups.

Glossary term

Programming paradigm that builds software by composing pure functions, treating computation as evaluating expressions rather than changing state.

Technology

React

Programming

Open-source JavaScript library from Meta for building user interfaces out of reusable components that update efficiently when data changes.

Glossary term

Geographically distributed network of servers that caches and delivers web content from locations close to users to improve speed and reliability.