Memoization
Memoization is a technique to cache computed values and skip unnecessary re-renders, improving performance by avoiding redundant work.React.memo
React.memo is a higher-order component that prevents re-renders when props haven’t changed. It performs a shallow comparison of props (see ReactMemo.js:12).
Implementation
FromReactMemo.js:12-29, React.memo wraps a component and optionally accepts a custom comparison function:
Basic Usage
Before: Unnecessary Re-renders
After: Optimized with memo
UserCard stays unchanged because its props haven’t changed.
Custom Comparison Function
By default,React.memo does shallow comparison. Provide a custom comparison for complex scenarios:
Display Name Handling
FromReactMemo.js:31-56, React automatically handles display names for debugging:
useMemo
useMemo caches expensive calculations, recomputing only when dependencies change (see ReactHooks.js:143).
Implementation
FromReactHooks.js:143-149:
Basic Usage
Before: Re-computing on Every Render
After: Optimized with useMemo
data or sortOrder actually changes, not on every render.
Referential Equality
UseuseMemo to maintain stable object references:
useCallback
useCallback memoizes functions, ensuring the same function reference across renders (see ReactHooks.js:135).
Implementation
FromReactHooks.js:135-141:
Basic Usage
Before: New Function on Every Render
After: Stable Function Reference
SearchResults doesn’t re-render when typing in the input field because handleResultClick maintains the same reference.
useCallback vs useMemo
useCallback is a shorthand for useMemo that returns a function:
When to Memoize
Good Candidates for Memoization
Poor Candidates for Memoization
Memoization Trade-offs
Memory vs Speed
Comparison Overhead
Dependency Management
Real-World Patterns
Combining Memoization Techniques
FromReactProfiler-test.internal.js:204, here’s a pattern combining React.memo with useMemo:
Memoizing Context Values
Optimizing Event Handlers
Debugging Memoization
Check if memoization works:
Using React DevTools
React DevTools Profiler shows why components re-rendered:- Open React DevTools
- Go to Profiler tab
- Record a session
- Click on a component in the flame graph
- Check “Why did this render?” to see which props changed