ReactMedium

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's preload, 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, useCallback manually).
  • Responsiveness: useTransition and useDeferredValue for heavy updates; debounce or throttle high-frequency events.
  • Large lists: virtualization (TanStack Virtual, react-window).
  • Avoid layout thrashing: batch DOM reads and writes; animate transform and opacity.
  • 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

tsx
1import { lazy, Suspense, useDeferredValue, useState } from "react";
2
3// 1. Code split heavy, rarely used UI
4const ReportsChart = lazy(() => import("./reports-chart"));
5
6// 2. Keep fast-changing state local
7function 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}
11
12export 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 responsive
16
17 const visible = rows.filter((r) => r.name.toLowerCase().includes(deferred.toLowerCase()));
18
19 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.

Open in study app