HomeReact
Chapter 21

21 — Performance Optimization

Techniques to make React apps fast. React is fast by default — optimize only when you measure a real problem.

The Performance Mindset

  1. Measure first — Don't optimize blindly
  2. Find the bottleneck — React DevTools Profiler
  3. Apply targeted fix — Not blanket memoization
  4. Verify improvement — Profile again

Optimization Techniques

TechniqueWhat It HelpsWhen to Use
React.memoSkip child re-rendersExpensive children, stable parent
useMemoSkip expensive computationsHeavy calculations, large datasets
useCallbackStable function referencesCallbacks passed to memo'd children
VirtualizationRender only visible itemsLong lists (1000+ items)
Code splittingSmaller initial bundleRoute-based, large components
Lazy loadingLoad on demandImages, below-fold content
Debounce/throttleReduce update frequencySearch input, scroll events
Key propControl component identityForce remount/avoid stale state

Common Performance Killers

ProblemFix
Re-rendering entire list when one item changesReact.memo + stable keys
Creating objects/arrays in renderuseMemo or lift outside
Inline functions as propsuseCallback
Large context re-renders all consumersSplit contexts
Rendering 10K itemsVirtualize with @tanstack/react-virtual

File

  • performance.tsx — memo patterns, virtualization, profiling, debounce, lazy images, state colocation