Reading — step 1 of 5
Read
~1 min readProduction
Performance Optimization
Why React can be slow:
- Every state update re-renders entire subtree.
- Diffing has O(n) cost per render.
- Many components, many re-renders.
Optimizations:
Memoization:
javascript
Skip render if props haven't changed (shallow compare by default).
useMemo:
javascript
Cache value across renders. Re-compute only if deps change.
useCallback:
javascript
Stable function reference. Prevents children memoized on this prop from re-rendering.
Lazy components:
javascript
Code-split. Load on demand.
Virtualization:
- For long lists (1000+ items): only render visible.
- Libraries: react-virtualized, react-window, TanStack Virtual.
Avoid inline objects in props:
javascript
Profiling:
- React DevTools Profiler.
- Why did this render? (Records render reason).
- Flame graph of render times.
Concurrent React (React 18+):
- Render in chunks; yield to browser.
- Suspense for streaming data.
- useTransition / useDeferredValue: mark updates as low-priority.
Server Components:
- Render on server; only ship HTML + interactivity bundle.
- Reduces JS bundle size dramatically.
- Next.js 13+ uses by default.
Other frameworks:
- Solid: no virtual DOM. Tracks fine-grained reactivity at compile time. Updates only the specific DOM node.
- Svelte: compile-time framework. No runtime overhead.
- Qwik: resumable hydration; tiny initial JS.
Modern JS is full of optimizations. The biggest wins come from architecture (data flow), not from micro-optimizing renders.
Don't optimize prematurely. Profile first.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…