Memoization rules
A decision procedure for memo, useMemo and useCallback that avoids caching for its own sake.
- #react
- #performance
- #memoization
Memoization is a cache, and every cache costs memory plus a comparison on each render. These rules decide when that trade is worth making.
Measure first
Open the React Profiler and confirm three things before adding a memo. The component re-renders, the re-render is slow, and the slowness shows on screen. Most suspected re-render problems turn out to be one expensive child rather than a cascade.
The rules
- Restructure before you memoize. Moving state down or lifting content up removes the re-render instead of making it cheaper.
useMemois for expensive computation or a stable reference, not for arithmetic. Memoizinga + bcosts more than recomputing it.useCallbackmatters only when the consumer compares. A handler passed to a plain DOM element gains nothing.memoneeds stable props. A memoized component handed a fresh object literal re-renders every time, and now pays for the comparison too.- Memoize the whole subtree or none of it. A memo boundary with one unstable prop inside it is overhead and nothing else.
A common mistake
Hoist the config to a module constant, or memoize it beside whatever produced rows.
Related entries
- List virtualization budget. For when the fix is rendering less rather than caching more.
- Render props. An inline function child defeats a memo boundary.
Related entries
List virtualization budget
Decide when windowing is worth its complexity, using node counts and a measured frame budget.
useIntersectionObserver hook
Track element visibility for lazy loading, infinite scroll and scroll-spy navigation.
Virtual list
Render only the visible slice of a long list, using fixed-height windowing and an absolute spacer.