← Back to Frontend, page 1

frontend-0008 · Medium mid

What does INP measure, and how do you shorten a slow click handler that hurts it?

INP measures the delay from a user input until the next frame is painted, and it counts input delay, handler time and rendering time. A slow handler is fixed by doing only the visible update first, then yielding to the browser so it can paint before the heavy work continues.

Full answer

For each click, tap or key press, the browser measures three phases:

  1. Input delay: the wait before the handler starts, because the main thread was busy.
  2. Processing time: how long the event handlers run.
  3. Presentation delay: the time to run style, layout and paint, and show the next frame.

INP is the interaction time near the slowest one in the visit. A page fails the metric when the 75th percentile of that value is above 200 milliseconds.

The most common cause is a handler that does the visible change and a big piece of unrelated work in one task. The browser cannot paint until the task ends. The fix is to split the work.

  • Do the change the user must see first, for example opening a panel or marking a button as pressed.
  • Yield to the main thread, so the browser can paint.
  • Run the remaining work after the yield.

setTimeout(fn, 0) is the old way to yield. scheduler.yield() is a newer way that keeps the continuation at a high priority, but you should check that the browser supports it and keep a fallback. Moving the work to a Web Worker is better still when it does not touch the DOM.

The page below has two buttons. The slow one blocks for about 300 milliseconds inside the click handler. The fast one updates the label first and yields before the same work. The Event Timing API reports the duration for each click. Durations are rounded to 8 milliseconds, so compare them with a margin.

A single yield only helps when the heavy part can wait. If the heavy part must run before the update, it needs to be made cheaper: less work, memoization, virtualization of long lists, or a worker.

html
<button id="slow">Slow</button>
<button id="fast">Fast</button>
<p id="label">idle</p>
<script>
  const work = () => { const end = performance.now() + 300; while (performance.now() < end) {} };
  slow.addEventListener('click', () => { label.textContent = 'slow done'; work(); });
  fast.addEventListener('click', async () => {
    label.textContent = 'fast done';           // the visible change first
    await new Promise((r) => setTimeout(r, 0)); // yield so the browser can paint
    work();                                     // the heavy part after
  });
  window.durations = {};
  new PerformanceObserver((list) => {
    for (const e of list.getEntries()) if (e.name === 'click') durations[e.target.id] = e.duration;
  }).observe({ type: 'event', durationThreshold: 16, buffered: true });
</script>

Run in headless Chrome 156 through Playwright. Two full runs gave the same results. Run with real mouse clicks. The slow handler reported about 300 ms and the fast one about 16 ms. Event Timing values are rounded to 8 ms, and real device speeds were not measured.

Key points

  • INP = input delay + processing time + presentation delay of the slowest typical interaction.
  • A handler runs in one task, so the browser cannot paint until it ends.
  • Do the visible update first, then yield, then do the rest.
  • Use scheduler.yield with a setTimeout fallback, or move work to a worker.
  • The Event Timing API shows which interactions are slow.

Common mistakes

  • Wrapping the heavy work in a Promise and expecting it to yield. A promise callback is a microtask and runs before painting.
  • Calling requestAnimationFrame once and doing the heavy work inside it, which still blocks the next frame.
  • Measuring only the handler time and ignoring input delay caused by another long task.
  • Optimizing a rare interaction instead of the slowest common one.

Follow-ups

  • Why does a resolved Promise not let the browser paint?
  • What would you check if the processing time is small but INP is still high?
  • How does a long list render affect presentation delay?

Tips

  • Describe the three phases before talking about fixes.
  • Say that field data decides which interaction to fix, and the profiler shows why.