Memoization
An optimization technique that caches the return value of expensive pure functions or component renders keyed by their inputs to skip redundant recomputation
Table of Contents
Structure Diagram
What is Memoization?
An optimization technique that caches the return value of expensive pure functions or component renders keyed by their inputs to skip redundant recomputation. It is a core concept in Performance & Optimization within Web Dev.
Core Principles
Memoization is a classic time-space tradeoff: it stores the output of an expensive pure function in memory keyed by its arguments, returning the cached result immediately on subsequent calls with the same inputs.
In modern frontend frameworks, memoization appears in React's useMemo, useCallback, and React.memo, as well as Vue's computed properties—preventing unnecessary recalculations and subtree reconciliations during re-renders.
Practical Takeaways
- Measure before memoizing: Wrapping trivial calculations in
useMemoadds dependency-comparison and memory overhead that can outweigh the savings. Focus on genuinely expensive derivations or preserving referential equality for memoized children and effect dependencies. - Watch unstable object/function references: Passing fresh inline literals (
style={{ ... }}) or callbacks defeats shallow equality checks inReact.memo. - Bound cache size for general functions: An unbounded memoization map over arbitrary inputs becomes a memory leak; cap it with an LRU eviction policy.
Notable Examples
- Derived table filtering and aggregation: Caching filtered/sorted datasets with
useMemoso unrelated UI state toggles don't re-run O(N log N) sorts. - State selectors (Reselect): Returning stable derived references from global stores to prevent cascading component re-renders.