Performance Optimization
State Performance Optimization covers unnecessary re-renders, Context value size, selector subscriptions, virtualization, and profile-driven memoization.
Course: React — State Management Lesson: 14
Learning objectives
Understand how state updates affect rendering, reduce unnecessary subscriptions and renders, use memoization appropriately, and measure state-related performance.
Performance starts with measurement
A component re-render is not automatically a performance problem. Use React DevTools Profiler and browser performance tools to identify slow commits, expensive calculations, large lists, or excessive network activity before optimizing.
Keep state close to consumers
State placed too high can cause a large part of the tree to re-render when it changes. Colocate state with the components that need it, and lift it only as far as required by shared consumers.
Split context by update frequency
A single context containing user profile, notification count, theme, and rapidly changing search state can notify many consumers for unrelated changes. Split contexts by responsibility and update patterns when profiling shows a benefit.
Select only the state a component needs
With Redux or Zustand, use focused selectors. A component that needs only cart.items.length should not subscribe to the entire application store. Be deliberate about selectors that allocate new arrays or objects.
Memoization
React.memo, useMemo, and useCallback can help avoid expensive repeated work or preserve identity across memoized boundaries. They also add complexity and do not fix poor state ownership or excessive data fetching.
const visibleOrders = useMemo(() => orders.filter(order => order.status === selectedStatus),[orders, selectedStatus]);
Use memoization when the calculation or downstream rendering cost is meaningful, and verify the result with profiling.
Large lists
For large tables and feeds, consider pagination or virtualization so the browser does not render thousands of off-screen rows. Server-side pagination also reduces payload size and memory usage.
Avoid duplicate requests
Use a server-state library's cache and deduplication features where appropriate. Debounce high-frequency search input and cancel obsolete requests. Ensure query keys accurately describe the requested data.
Performance checklist
- Profile before optimizing.
- Keep state close to its consumers.
- Use focused selectors.
- Split contexts by responsibility when beneficial.
- Avoid derived state duplication.
- Paginate or virtualize large collections.
- Cache remote data with correct keys.
- Debounce and cancel high-frequency requests.
- Re-measure after changes.
Common mistakes
- Adding
useMemoanduseCallbackeverywhere. - Assuming every re-render is expensive.
- Using global state for local interactions.
- Rendering huge lists without pagination or virtualization.
- Optimizing React while ignoring slow APIs or large payloads.
Interview questions & answers
Interview preparation
5 questions grouped by difficulty — expand any card for a model answer and follow-up probe.
Beginner
1How do you identify unnecessary re-renders?
BeginnerModel answer
Profile with React DevTools, confirm state/prop identity changes, and measure before adding memoization.
2When is virtualization useful?
BeginnerModel answer
When rendering large lists where most rows are offscreen; virtualization mounts only visible items.
Intermediate
3Why can a large Context value cause rendering work?
IntermediateModel answer
Any provider value change re-renders all consumers; a fat/unstable value amplifies work across the tree.
4Why should memoization be driven by profiling?
IntermediateModel answer
BlinduseMemo/React.memocan add overhead; profile first so you optimize real hotspots.
Advanced
5What is the purpose of selector-based subscriptions?
AdvancedModel answer
Components subscribe to narrow slices so unrelated store updates do not re-render them.
Practice
Profile an order dashboard with filters, summary cards, and a large table. Identify one rendering bottleneck and one data-fetching bottleneck, implement targeted improvements, and compare before/after measurements.