Lesson 172 of 296
React performance
What you will learn
- How to find performance problems before fixing them
- React.memo
- Avoiding unnecessary renders with state colocation and composition
- Lazy loading and code splitting
- Long lists and virtualisation
- useTransition and useDeferredValue
- Finding and fixing a slow list render
On this page
Measure first
Do not optimise by guessing. Use:
- React DevTools Profiler: records renders, shows which components rendered, why, and how long they took.
- The browser Performance panel (Lesson 86): long tasks, layout and paint.
- Lighthouse and Core Web Vitals (INP shows slow interactions, Lesson 86).
- React 19.2 also adds Performance Tracks, showing React's work inside the browser's Performance panel.
React.memo
React.memo wraps a component so it skips re-rendering when its props are the same as last time (compared with Object.is for each prop, a shallow comparison):
import { memo } from "react";
const PriceRow = memo(function PriceRow({ name, pricePence }: { name: string; pricePence: number }) {
return <li>{name}: {pricePence}p</li>;
});It only helps if the parent passes stable props. A new object, array or function prop every render defeats it, which is where useMemo and useCallback (Lesson 168) come in.
Avoiding renders without memo
Often the best fix is structure, not memoisation:
1. Move state down (colocation). If typing in a search box makes a whole page re-render, move the search state into a small SearchBox component, so only it re-renders.
2. Lift content up (pass children). A component with frequently changing state can receive expensive content as children. The children were created by the parent, so they do not re-render when this component's state changes:
function ScrollTracker({ children }: { children: ReactNode }) {
const [scrollY, setScrollY] = useState(0);
useEffect(() => {
const onScroll = () => setScrollY(window.scrollY);
window.addEventListener("scroll", onScroll, { passive: true });
return () => window.removeEventListener("scroll", onScroll);
}, []);
return (
<div>
<p>Scrolled {Math.round(scrollY)}px</p>
{children}
</div>
);
}Code splitting and lazy loading
Load components only when needed (Lesson 73), so the first page loads faster:
import { lazy, Suspense } from "react";
const ReportsPage = lazy(() => import("./ReportsPage"));
function AdminArea({ showReports }: { showReports: boolean }) {
return (
<Suspense fallback={<p>Loading reports...</p>}>
{showReports && <ReportsPage />}
</Suspense>
);
}lazy takes a function that returns a dynamic import(). Suspense (Lesson 173) shows the fallback while the code downloads. Frameworks like Next.js split code by route automatically.
Long lists: virtualisation
Rendering 10,000 rows creates 10,000 DOM nodes: slow to render and to scroll. Virtualisation (also called windowing) renders only the rows visible on screen plus a few extra, and swaps them as you scroll. Libraries: TanStack Virtual, react-window. Pagination or infinite scrolling (Part 14) are alternatives.
Keeping typing responsive
When a fast update (typing) triggers a slow one (filtering a big list), mark the slow part as non-urgent:
import { useDeferredValue } from "react";
function FilterableList({ rows }: { rows: Row[] }) {
const [query, setQuery] = useState("");
const deferredQuery = useDeferredValue(query);
return (
<>
<input aria-label="Filter" value={query} onChange={(e) => setQuery(e.target.value)} />
<ProductSearch rows={rows} query={deferredQuery} />
</>
);
}useDeferredValuegives a version of the value that may lag behind; React updates the input immediately and renders the expensive list with the deferred value when it can (concurrent rendering, Lesson 163). Combine withmemoon the expensive child for full benefit.useTransitiondoes the same for state updates:startTransition(() => setTab("reports"))marks an update as non-urgent and gives anisPendingflag.
Worked example: fixing a slow list
Symptom: typing in the filter box of a 5,000-row product table lags.
Investigation with the Profiler: every keystroke re-renders ProductTable and all 5,000 row components, and filtering plus sorting runs each time.
Fixes, in order:
- Memoise the filtered list with
useMemoon[rows, query]. - Wrap each row component in
memo, and make sure its props are primitives or stable references (passonSelectthroughuseCallback). - Debounce or defer the query (
useDeferredValue) so typing stays instant. - If still slow, virtualise the table so only about 30 rows exist in the DOM.
- Measure again after each change, and keep only the changes that helped.
Other common causes of slowness
- Context values that change too often (Lesson 169).
- Creating components inside components (Lesson 163).
- Large images without sizes (Lesson 8) and too much JavaScript on first load.
- Effects that set state and trigger extra renders (Lesson 166).
How to say it in an interview
"I measure first with the React Profiler and the Performance panel. Many problems are fixed by structure: moving state down closer to where it is used, or passing expensive content as children so it does not re-render. React.memo skips renders when props are shallowly equal, which needs stable props from useMemo and useCallback. I split code with lazy and Suspense, virtualise long lists, and use useDeferredValue or useTransition to keep typing responsive during expensive renders. For a slow list I memoise the filtering, memoise rows, defer the query and virtualise if needed, measuring after each step."
Key words
| Word | Meaning |
|---|---|
| Profiler | A tool that records what rendered and how long it took |
| React.memo | Skips re-rendering when props have not changed |
| Shallow comparison | Comparing each prop with Object.is, not deeply |
| Code splitting | Loading parts of the app only when needed |
| Virtualisation | Rendering only the visible rows of a long list |
| useDeferredValue / useTransition | Marking updates as non-urgent |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.