ReactMedium

How to trace performance issues?

Trace performance issues by measuring first: use field data (Core Web Vitals such as INP and LCP from real users) to find which pages and interactions are slow, reproduce them, record with the Chrome DevTools Performance panel and the React DevTools Profiler to see whether time goes to rendering, JavaScript, network, layout or paint, then fix the biggest cost and measure again.

Step by step

  1. Identify: real-user monitoring (web-vitals library, Vercel Speed Insights, Sentry, Datadog) and Lighthouse reports. INP measures responsiveness, LCP loading, CLS layout stability.
  2. Reproduce in a production build, with CPU throttling (4x to 6x) to simulate mid-range phones.
  3. Record:
    • Chrome Performance panel: long tasks (over 50ms), layout thrashing, scripting time. React 19.2 adds React-specific tracks (Scheduler priorities, component renders and effects).
    • React Profiler: which components rendered, how long, and why.
    • Network panel: request waterfalls, large bundles, uncached assets.
  4. Diagnose common causes:
    • Too many or too expensive renders (unstable props, broad context, state too high).
    • Huge lists without virtualization.
    • Heavy synchronous work in event handlers (move to transitions, workers, or chunk it).
    • Data waterfalls (fetch in parallel, prefetch, use loaders).
    • Large bundles (code split, analyze).
    • Layout shifts from images without dimensions or late-loading fonts.
  5. Fix and verify with the same measurement.

Useful APIs

performance.mark/measure, the Long Animation Frames API, PerformanceObserver, and React's <Profiler>.

Code example

tsx
1// main.tsx - collect real-user metrics with the web-vitals library
2import { onCLS, onINP, onLCP, type Metric } from "web-vitals/attribution";
3
4function report(metric: Metric) {
5 const body = JSON.stringify({
6 name: metric.name,
7 value: Math.round(metric.value),
8 rating: metric.rating,
9 page: location.pathname,
10 // attribution points at the slow element or interaction
11 target: "attribution" in metric ? (metric.attribution as { interactionTarget?: string; target?: string }).interactionTarget ?? (metric.attribution as { target?: string }).target : undefined,
12 });
13 navigator.sendBeacon("/api/vitals", body);
14}
15
16onINP(report);
17onLCP(report);
18onCLS(report);
19
20// Custom timing around a suspicious operation
21export function measureSort<T>(label: string, items: T[], compare: (a: T, b: T) => number) {
22 performance.mark(`${label}-start`);
23 const sorted = items.toSorted(compare);
24 performance.measure(label, `${label}-start`); // visible in the Performance panel
25 return sorted;
26}

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