← Back to React, page 5

react-0090 · Hard senior

How would you diagnose and fix a React page with poor Interaction to Next Paint (INP)?

Measure first: use field data (web-vitals, CrUX) and the Performance panel to find which interactions are slow and whether the delay is input delay, processing time or presentation. Use the React Profiler to find expensive renders and the flame chart to find long tasks. Then fix the cause: avoid unnecessary renders, move heavy work out of the handler, mark non-urgent updates as transitions, split long tasks by yielding to the main thread, and reduce DOM size or list length.

Full answer

INP measures the delay between a user interaction (click, tap, key) and the next frame painting, and is reported at roughly the worst interaction on a page visit. A good value is 200 ms or less. It is a field metric, so start with real-user data to find the pages and interactions that are bad.

Reproduce in the browser Performance panel with CPU throttling, because developer machines hide the problem. Find the interaction, and look at three parts: input delay (the main thread was busy before the event), processing time (your handlers and React's render and commit), and presentation delay (style, layout and paint). A long yellow bar of scripting under the interaction points to JavaScript. Layout and paint cost points to DOM size or expensive CSS.

React-specific tools: the React DevTools Profiler shows which components rendered, why (props, state, context, parent) and how long each took. Look for components that render on every keystroke without needing to, very large lists, expensive calculations in render and context values that change too often.

Fixes, in order of impact:

  1. Do less. Colocate state so a keystroke re-renders a small subtree. Memoize expensive children with memo when props are stable (or rely on React Compiler). Derive values instead of storing them. Virtualize long lists.
  2. Defer. Wrap non-urgent updates in startTransition or use useDeferredValue so typing stays responsive while heavy results render in the background.
  3. Split long tasks. Break large synchronous work into chunks and yield to the main thread between them with scheduler.yield() (with a setTimeout fallback), so the browser can handle input and paint. Move CPU-heavy work to a Web Worker.
  4. Reduce what ships: lazy-load rarely used code so parsing and hydration cost less.
  5. Make the first feedback immediate, for example a pressed state, and then do the work.

Verify with the same trace and track the metric after release. The example proves the yielding idea with an event that is waiting while heavy work runs.

jsx
const yieldToMain = () =>
  globalThis.scheduler && typeof globalThis.scheduler.yield === 'function'
    ? globalThis.scheduler.yield()
    : new Promise((resolve) => setTimeout(resolve, 0));

function heavyStep(n) {
  let total = 0;
  for (let i = 0; i < 200000; i++) total += Math.sqrt(i + n);
  return total;
}

async function blockingRun(steps) {
  for (let i = 0; i < steps; i++) heavyStep(i);
  console.log('blocking run finished');
}

async function chunkedRun(steps) {
  for (let i = 0; i < steps; i++) {
    heavyStep(i);
    if (i % 5 === 4) await yieldToMain();
  }
  console.log('chunked run finished');
}

// A queued user event stands in for a click that arrives while work is running.
setTimeout(() => console.log('click handled during the blocking run'), 0);
await blockingRun(40);
await yieldToMain();

setTimeout(() => console.log('click handled during the chunked run'), 0);
await chunkedRun(40);

This ran as a script. A timer represents the user's click. In the blocking run it waits until all work ends, and in the chunked run it is handled after the first chunk, before the rest of the work. Output of the run: blocking run finished click handled during the blocking run click handled during the chunked run chunked run finished

Key points

  • INP is a field metric. Find slow interactions with real-user data, then reproduce with throttling.
  • Break the delay into input delay, processing time and presentation delay.
  • Use the React Profiler to find unnecessary or expensive renders.
  • Fix by doing less, deferring with transitions, splitting long tasks, and using workers.
  • Verify with a trace and monitor after release.

Common mistakes

  • Optimizing from guesses instead of a trace.
  • Testing only on a fast development machine without throttling.
  • Memoizing everything without measuring.
  • Doing heavy work inside the event handler before any visual feedback.
  • Ignoring DOM size and long lists as causes.

Follow-ups

  • What is the difference between INP and First Input Delay?
  • How would you move a heavy calculation into a Web Worker?
  • How does the React Compiler change your approach to memoization?

Tips

  • Start with measurement. It is what separates seniors from guessers.
  • Name the three parts of an interaction delay.
  • List fixes in order of cost and impact.