Performance Optimization
React is fast by default, but complex applications can slow down. This chapter covers techniques to identify bottlenecks and optimize your React apps.When to Optimize
Understanding Re-renders
Every time state or props change, React re-renders components. Understanding this is key to optimization.What Causes Re-renders?
- Component’s state changes (
useState,useReducer) - Parent component re-renders
- Context value changes
- Custom hook state changes
React DevTools Profiler
The Profiler helps identify what’s rendering and why.Using the Profiler
- Install React DevTools browser extension
- Open DevTools → Profiler tab
- Click “Record” and interact with your app
- Analyze the flame graph
What to Look For
- Frequent re-renders of the same component
- Slow components (long render times)
- Cascade re-renders (parent update causing all children to update)
React.memo - Prevent Unnecessary Re-renders
React.memo is a higher-order component that memoizes functional components.
Basic Usage
Custom Comparison
useMemo - Memoize Expensive Calculations
useMemo caches the result of expensive computations.
When to Use useMemo
| Use useMemo | Don’t Use useMemo |
|---|---|
| Expensive calculations | Simple math/string operations |
| Large array operations | Small arrays (fewer than 100 items) |
| Object creation for memoized children | Objects used once |
| Derived data from props/state | Values passed to native elements |
useCallback - Memoize Functions
useCallback caches function references to prevent unnecessary re-renders of child components.
Code Splitting with React.lazy
Split your bundle so users only download what they need.Named Exports with Lazy
Virtualization for Long Lists
Only render visible items in long lists.Using react-window
Variable Size List
Debouncing and Throttling
Limit how often functions are called.Debouncing (Wait for pause)
Throttling (Limit frequency)
Image Optimization
Lazy Loading Images
Progressive Loading with Blur
Web Vitals
Key metrics for user experience:| Metric | Description | Target |
|---|---|---|
| LCP (Largest Contentful Paint) | Main content visible | < 2.5s |
| FID (First Input Delay) | Time to interactive | < 100ms |
| CLS (Cumulative Layout Shift) | Visual stability | < 0.1 |
| TTFB (Time to First Byte) | Server response time | < 600ms |
Measuring Web Vitals
Production Build Optimization
Vite Production Build
Analyze Bundle Size
Environment-Specific Code
Deployment
Vercel (Recommended for React)
Netlify
Createnetlify.toml:
Docker
🎯 Practice Exercises
Exercise 1: Optimize a Slow List
Exercise 1: Optimize a Slow List
Summary
| Technique | Use Case |
|---|---|
| React.memo | Prevent child re-renders when props unchanged |
| useMemo | Cache expensive calculations |
| useCallback | Cache function references |
| React.lazy | Code split by route/component |
| Virtualization | Long lists (100+ items) |
| Debouncing | Search inputs, resize handlers |
| Lazy loading | Images below the fold |
| Bundle analysis | Identify large dependencies |
| Web Vitals | Measure real user experience |
Next Steps
In the next chapter, you’ll learn about Authentication & Protected Routes — securing your React applications!