What is React performance optimization?
React performance optimization is making apps load fast and respond instantly, by shipping less JavaScript, rendering less and less often, keeping the main thread free for user input, and fetching data efficiently. Start by measuring (Profiler, Lighthouse, Core Web Vitals), then fix the biggest bottleneck.
Loading performance
- Ship less code: route-based code splitting (
lazy, framework routing), tree shaking, lighter dependencies. - Server rendering: SSR, static generation and Server Components send ready HTML and less JavaScript.
- Assets: responsive, modern-format images,
font-display, preloading critical resources (React 19'spreload,preinit). - Data: avoid request waterfalls; fetch in parallel, prefetch on hover, cache with TanStack Query or SWR.
Runtime performance
- Fewer re-renders: colocate state, pass
children, split contexts, use selectors in stores. - Cheaper renders: memoization with the React Compiler (or
memo,useMemo,useCallbackmanually). - Responsiveness:
useTransitionanduseDeferredValuefor heavy updates; debounce or throttle high-frequency events. - Large lists: virtualization (TanStack Virtual,
react-window). - Avoid layout thrashing: batch DOM reads and writes; animate
transformandopacity. - Offload work: Web Workers for heavy computation.
Metrics to track
LCP (loading), INP (responsiveness, replaced FID in 2024) and CLS (layout stability).
Tools
React DevTools Profiler, Chrome Performance panel with React's Scheduler and Components tracks (React 19.2), Lighthouse, bundle analyzers, and real-user monitoring.
Principle
Optimize what you can measure; most components do not need optimization.
Code example
1import { lazy, Suspense, useDeferredValue, useState } from "react";23// 1. Code split heavy, rarely used UI4const ReportsChart = lazy(() => import("./reports-chart"));56// 2. Keep fast-changing state local7function SearchBox({ onChange }: { onChange: (q: string) => void }) {8 const [q, setQ] = useState("");9 return <input value={q} onChange={(e) => { setQ(e.target.value); onChange(e.target.value); }} />;10}1112export function Dashboard({ rows }: { rows: { id: string; name: string }[] }) {13 const [query, setQuery] = useState("");14 const [showChart, setShowChart] = useState(false);15 const deferred = useDeferredValue(query); // 3. keep typing responsive1617 const visible = rows.filter((r) => r.name.toLowerCase().includes(deferred.toLowerCase()));1819 return (20 <div>21 <SearchBox onChange={setQuery} />22 <p>{visible.length} matches</p>23 <button onClick={() => setShowChart(true)}>Show chart</button>24 {showChart && (25 <Suspense fallback={<p>Loading chart…</p>}>26 <ReportsChart />27 </Suspense>28 )}29 </div>30 );31}
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.