useMemo & useCallback: Performance
Premature memoization can add complexity with negligible benefit. Strong engineers optimize measured bottlenecks and reason about cache invalidation boundaries.
Quick Navigation: Cost Model First • When It Helps vs Hurts • Trade-offs and Interview Takeaway
Quick Decision Guide
useMemo caches values; useCallback caches function identity.
Use when: - expensive computations dominate render cost - child memoization depends on stable props
Interview signal: tie memoization to a concrete cost model, not habit.
Cost Model First
🔥 Insight
Optimization starts with identifying dominant cost, not by sprinkling hooks.
🧠Mental Model
without memo: compute on every render
with memo: compare deps + compute only on invalidationMemoization has overhead (dependency comparison, cache storage, cognitive load).
When It Helps vs Hurts
const filtered = useMemo(() => heavyFilter(items, query), [items, query])
const onSelect = useCallback((id) => select(id), [select])Helps
Hurts
Rules of Hooks (and why)
These rules follow from React's render model:
Trade-offs and Interview Takeaway
Trade-offs
🎯 Interview Takeaway
State the bottleneck, show why memoization targets it, and mention invalidation/dependency risk.