Performance Optimization
React is fast by default, but as your application grows, you may encounter performance bottlenecks. This guide covers strategies for identifying and resolving common performance issues.Production Builds
Always use production builds for deployed applications. Development builds include helpful warnings and debugging tools but are significantly slower.Avoiding Unnecessary Renders
One of the most common performance issues is components re-rendering when their output hasn’t changed.Before: Unnecessary Re-renders
After: Optimized with React.memo
React DevTools Profiler
The React DevTools Profiler helps you visualize component render performance.Using the Profiler
- Install React DevTools
- Open the Profiler tab
- Click the record button
- Interact with your app
- Stop recording to analyze results
What to Look For
- Flame graphs: Visualize which components took the longest to render
- Ranked charts: See components sorted by render time
- Component charts: Track individual component render counts
- Yellow/orange bars: Indicate slower renders that may need optimization
Common Performance Issues
1. Creating New Objects/Functions in Render
Problem: Creating new object references on every render prevents memoization.useMemo and useCallback to maintain stable references.
2. Large Lists Without Keys
Problem: React can’t efficiently update lists without stable keys.3. Expensive Calculations in Render
Problem: Heavy computations run on every render.4. Context Updates Causing Wide Re-renders
Problem: Context updates cause all consumers to re-render.PureComponent for Class Components
For class components, extendPureComponent instead of Component for automatic shallow prop comparison (see ReactBaseClasses.js:132).
Code Splitting
Split large bundles usingReact.lazy() and dynamic imports.
Virtualization for Long Lists
For lists with hundreds or thousands of items, render only visible items.Measuring Performance
Browser DevTools
Use the Performance tab in Chrome DevTools:- Open DevTools (F12)
- Go to Performance tab
- Click Record
- Interact with your app
- Stop recording and analyze the flame chart
User Timing API
Optimization Checklist
- Use production builds for deployed apps
- Profile with React DevTools to identify slow components
- Memoize expensive calculations with
useMemo - Memoize callback functions with
useCallback - Wrap expensive components with
React.memo - Use stable keys for lists
- Split large contexts into smaller ones
- Implement code splitting for large components
- Virtualize long lists
- Measure improvements with real user metrics