日本語

Improving INP in Core Web Vitals by Splitting Heavy Work

​​Core Web Vitals​Chrome,JavaScript,Playwright,scheduler.yield,web-vitals

When you run heavy work all at once inside a click handler, the screen does not change until that work finishes, and INP (Interaction to Next Paint) in Core Web Vitals gets worse. On the page I tested, a single task that totaled 300ms gave an INP of 304ms. After updating the display first and splitting the remaining work with scheduler.yield(), INP dropped to 64ms. Splitting it into smaller pieces brought it down to 24ms. This fix applies when a "Save" or "Search" button responds slowly and PageSpeed Insights reports INP as "needs improvement."

Note

The test environment was Ubuntu 24.04, Node.js 22, and web-vitals 5.3.0. For the browser, I used Chrome for Testing 153, which ships with Playwright 1.63.0, in headless mode. I served the page from a server running on localhost:8080 and opened it at a 412px by 823px viewport. I simulated heavy work with a loop that keeps the main thread busy for a set amount of time. I measured each value twice, and unless noted otherwise, the numbers are from the first run. I did not test real user data (PageSpeed Insights field data or Search Console), CPU throttling settings, browsers other than Chrome, or behavior in browsers that do not support scheduler.yield().

INP is the time from pressing to the next paint

According to web.dev's explanation of INP, INP looks at the latency of every click, tap, and key interaction while the page is open, and reports the slowest one (on pages with more than 50 interactions, it ignores the single slowest interaction for every 50). 200ms or less is "good," 500ms or less is "needs improvement," and anything slower is "poor." In web-vitals 5.3.0, the boundaries are defined as INPThresholds = [200, 500].

web.dev's guide to improving INP splits the time of one interaction into three parts:

  • Input delay: the time an event waits because the main thread is busy with other work and cannot start handling it
  • Processing time (processingDuration in web-vitals): the time the event listeners take to run
  • Presentation delay: the time from when the processing finishes until the next frame is painted

web.dev's explanation of long tasks calls any task longer than 50ms a Long task. When a button handler becomes a long task, that interaction's processing time grows, and any other interaction that happens during it also waits as input delay.

Before the fix, INP was 304ms

The test page had a memo input and a "Save" button. When the button is pressed, it runs five steps in sequence (each about 60ms) and then shows "Saved." I left out smallSteps in common.js (used in the last section) and the HTML head. The later pages show only the click listener.

js
// common.js
// Stand-in for heavy work: keep the main thread busy for the given milliseconds
function busy(ms) {
  const end = performance.now() + ms;
  while (performance.now() < end) {}
}
// Five steps run when the Save button is pressed (each about 60ms)
const steps = [
  () => busy(60), // Validate input
  () => busy(60), // Sort the list
  () => busy(60), // Aggregate
  () => busy(60), // Prepare data for saving
  () => busy(60), // Prepare analytics submission
];
html
<p><input id="memo" placeholder="メモ" style="font-size:20px"></p>
<button id="save">保存する</button>
<p id="status">まだ保存していません</p>
<script src="common.js"></script>
<script>
const status = document.getElementById('status');
document.getElementById('save').addEventListener('click', () => {
  for (const step of steps) step();
  status.textContent = '保存しました';
});
</script>

For measurement, I used a Playwright script (measure.cjs) that opens the page and interacts with it. It presses the button three times and reports the approximate time until "Saved" appears (from Playwright's click until it finds the text), along with per-interaction timings collected by a PerformanceObserver (Event Timing) and the Long Tasks records. On the fourth run, I clicked the memo field 100ms after pressing the button, typed one character "a", and measured how long that interaction was held up during processing. Finally, it prints the INP reported by web-vitals onINP (the build with attribution) and its breakdown. I left out the events: lines from the output.

console
$ node measure.cjs before yield settimeout yield-late yield-small
== before ==
click 1: interaction 304ms, '保存しました' まで約369ms, long tasks [300]
click 2: interaction 304ms, '保存しました' まで約336ms, long tasks [300]
click 3: interaction 304ms, '保存しました' まで約338ms, long tasks [300]
memo while saving (value "a"): pointerdown 304ms(delay 4, proc 0), pointerup 304ms(delay 4, proc 0), click 304ms(delay 4, proc 300), pointerdown 208ms(delay 204, proc 0), pointerup 208ms(delay 204, proc 0), click 208ms(delay 204, proc 0)
web-vitals onINP: 304ms rating=needs-improvement (inputDelay 1, processingDuration 301, presentationDelay 3, target #save)
…

Each button press produced one 300ms long task, and the interaction took 304ms. INP was also 304ms, rated needs-improvement. Most of the breakdown is processing time (processingDuration 301), which shows the time was spent inside the event listener.

The memo field I clicked during processing waited 204ms before its event handling started (delay 204). I clicked 100ms after the button press, so it waited only for the rest of the 300ms task.

Update the display first, then split the work with scheduler.yield

web.dev's guide to improving INP recommends splitting event listener work into separate tasks rather than one long task. The MDN explanation of scheduler.yield() includes an example where the click listener first shows that the request was accepted, then does the heavy work after await scheduler.yield(). I changed the code to this form.

js
document.getElementById('save').addEventListener('click', async () => {
  status.textContent = '保存しています…';
  for (const step of steps) {
    await scheduler.yield();
    step();
  }
  status.textContent = '保存しました';
});

After changing the display to "Saving…", the listener yields the main thread. The browser paints the screen at that point, and then the five steps continue, each as a separate task.

console
== yield ==
click 1: interaction 16ms, '保存しました' まで約358ms, long tasks [60,60,60,60,60]
click 2: interaction 16ms, '保存しました' まで約339ms, long tasks [60,60,60,60,60]
click 3: interaction 16ms, '保存しました' まで約342ms, long tasks [60,60,60,60,60]
memo while saving (value "a"): pointerdown 24ms(delay 21, proc 0), pointerup 24ms(delay 22, proc 0), click 24ms(delay 22, proc 0), keydown 64ms(delay 59, proc 0), keypress 64ms(delay 59, proc 1), keyup 56ms(delay 60, proc 0)
web-vitals onINP: 64ms rating=good (inputDelay 60, processingDuration 1, presentationDelay 3, target #memo)

The button press interaction dropped from 304ms to 16ms. The long tasks split into five tasks of 60ms each. However, the time until "Saved" appeared was about 340 to 360ms, almost the same as before the fix. The amount of work did not change, so the total did not get faster. The only change is that the screen can update earlier.

Page INP dropped from 304ms to 64ms, rated good. But this 64ms was not from the button. It came from the memo input (target #memo) that happened during processing. The key input arrived while one of the 60ms chunks was running, and it waited for that chunk to finish, about 60ms (inputDelay 60).

Splitting with setTimeout gave the same numbers

I also tried splitting with a function that wraps setTimeout in a Promise, as shown in web.dev's explanation of long tasks, instead of scheduler.yield().

js
function yieldToMain() {
  return new Promise((resolve) => setTimeout(resolve, 0));
}
document.getElementById('save').addEventListener('click', async () => {
  status.textContent = '保存しています…';
  for (const step of steps) {
    await yieldToMain();
    step();
  }
  status.textContent = '保存しました';
});
console
== settimeout ==
click 1: interaction 16ms, '保存しました' まで約360ms, long tasks [60,60,60,60,60]
click 2: interaction 16ms, '保存しました' まで約341ms, long tasks [60,60,60,60,60]
click 3: interaction 16ms, '保存しました' まで約341ms, long tasks [60,60,60,60,60]
memo while saving (value "a"): pointerdown 24ms(delay 21, proc 0), pointerup 24ms(delay 22, proc 0), click 24ms(delay 22, proc 0), keydown 64ms(delay 60, proc 0), keypress 64ms(delay 60, proc 1), keyup 64ms(delay 61, proc 0)
web-vitals onINP: 64ms rating=good (inputDelay 60, processingDuration 1, presentationDelay 3, target #memo)

The button press was 16ms and INP was 64ms, the same as with scheduler.yield(). According to the web.dev explanation, with scheduler.yield() the remaining work keeps its original place in the queue after the yield, so third-party scripts cannot jump in between. On this test page, no other scripts were running, so the difference did not show up in the numbers. You can check whether scheduler.yield() is available by testing globalThis.scheduler?.yield, as described on MDN.

Heavy work before the display update delays the button response

I also measured a version that runs one step first and then calls scheduler.yield(), without showing the "Saving…" message.

js
document.getElementById('save').addEventListener('click', async () => {
  for (const step of steps) {
    step();
    await scheduler.yield();
  }
  status.textContent = '保存しました';
});
console
== yield-late ==
click 1: interaction 64ms, '保存しました' まで約364ms, long tasks [61,60,60,60,60]
click 2: interaction 64ms, '保存しました' まで約331ms, long tasks [60,60,60,60,60]
click 3: interaction 64ms, '保存しました' まで約326ms, long tasks [60,60,60,60,60]
…
web-vitals onINP: 64ms rating=good (inputDelay 1, processingDuration 61, presentationDelay 3, target #save)

The button press took 64ms, and the breakdown was processing time (processingDuration 61). The first step, which ran before the first await, counts as the listener's processing time. INP is 64ms, the same as the two above, but here the delay comes from the button itself. Also, from the button press until "Saved" appeared, which took about 330 to 360ms, the screen did not change at all. As the web.dev guide to improving INP says, you should first do only the display updates the user needs to see next, and leave everything else for later tasks.

Smaller pieces also shortened the wait for other interactions during processing

To check whether the wait for other interactions during processing depends on the length of each piece, I measured a page that split the same 300ms total of work into 15 pieces of about 20ms each. The listener is the same as the scheduler.yield() page, except it loops over smallSteps instead of steps.

js
// The same amount of work (300ms total), split into 15 pieces of about 20ms each
const smallSteps = Array.from({ length: 15 }, () => () => busy(20));
console
== yield-small ==
click 1: interaction 16ms, '保存しました' まで約351ms, long tasks []
click 2: interaction 16ms, '保存しました' まで約341ms, long tasks []
click 3: interaction 16ms, '保存しました' まで約341ms, long tasks []
memo while saving (value "a"): keydown 24ms(delay 19, proc 0), keypress 24ms(delay 19, proc 0), keyup 24ms(delay 20, proc 0)
web-vitals onINP: 24ms rating=good (inputDelay 19, processingDuration 1, presentationDelay 4, target #memo)

No task reached 50ms, so no long task records were produced. The key input during processing waited about 20ms, and INP was 24ms. In the second run, the presentation delay was 16ms, so INP was 40ms. Both runs were shorter than the 64ms from splitting into 60ms pieces.

Values before and after the fix

Here are the values that web-vitals onINP reported in the first run.

Horizontal bar chart comparing INP across five ways of writing the save button page. Before the fix, INP was 304ms, above the 200ms line. Splitting the work into five pieces with scheduler.yield or setTimeout after updating the display, and splitting without updating the display first, each gave 64ms. Splitting into 15 pieces of about 20ms each gave 24ms
INP measured with web-vitals onINP. The total work is about 300ms in every case
Page Button press interaction Page INP Interaction reported as INP Long tasks
One task (before the fix) 304ms 304ms Button One task of 300ms
Update display first, split into five with scheduler.yield() 16ms 64ms Memo input during processing Five tasks of 60ms
Update display first, split into five with setTimeout 16ms 64ms Memo input during processing Five tasks of 60ms
Without updating display, scheduler.yield() after each step 64ms 64ms Button Five tasks of 60ms
Update display first, split into 15 pieces of about 20ms 16ms 24ms Memo input during processing None

In the second run, INP was 304ms before the fix and 64ms for the three five-piece pages, the same as the first run.

Point

Splitting the heavy work does not change the time until "Saved" appears, which stays around 340ms. INP improves because the screen can be updated first on each interaction, and other interactions during processing wait only for one piece of the work.

In the measurements, I passed durationThreshold: 16 to web-vitals onINP. According to the web-vitals 5.3.0 README and the library code (onINP.js), the default is 40ms, and interactions shorter than that are not counted in INP (the first interaction is always counted, even if it is short). The default would still catch delays of 40ms or more, but I lowered it so I could compare small post-fix values such as 16ms and 24ms.

​Posted by 86