日本語

How Modal Open and Close Are Measured in Core Web Vitals INP

​​Core Web Vitals​Chrome,dialog element,Event Timing API,JavaScript,Playwright,web-vitals

The INP (Interaction to Next Paint) for opening a modal is the time from the button press until the modal is first painted. The length of the opening animation is not included, and closing is measured as a separate interaction. This article explains how to read INP values on pages that show confirmation dialogs, login forms, or image lightboxes as modals.

In my test environment, opening a light modal with showModal() on a <dialog> element gave an INP of 8ms to 24ms. When I increased the page to about 40,000 elements, the open interaction took 88ms to 104ms and the close interaction took 128ms to 152ms, and the close interaction was reported as the INP. On the same page, show() and toggling a div took 32ms to 48ms.

Note

The test environment was Ubuntu 24.04, Node.js 22, web-vitals 6.2.3, and Playwright 1.63.0 with Chromium 158 (headless). I served the page from a server I ran locally (localhost:8080), used a 412px-wide viewport, and clicked the open button with the mouse. After one second, I closed the modal by clicking the "OK" button inside it or by pressing the Esc key, and I measured each case five times. I did not test tap input, real smartphones, browsers other than Chrome, modals built with libraries such as React, or data from real users (CrUX or PageSpeed Insights field data).

The open interaction is measured until the modal is first painted

According to web.dev's explanation of INP, INP represents the slowest of the clicks, taps, and key presses made on a page (when there are more than 50 interactions, the slowest one is dropped for every 50). The time runs from the interaction until the next frame is painted after the event handling finishes. A value of 200ms or less is "good," and 500ms or less is "needs improvement."

For the test, I prepared a page where pressing a button simply calls showModal(). The rest of the page was a 300-row list standing in for an article list, bringing the total to 642 elements.

js
$('open-light').addEventListener('click', () => $('dlg-light').showModal());

The open interaction for this "light modal" had an INP of 8ms to 24ms over five runs (median 16ms). As described in MDN's explanation of PerformanceEventTiming, Event Timing API times are rounded to 8ms steps. At this speed, the values are therefore always one of 8ms, 16ms, or 24ms.

Adding a 400ms opening animation (the modal rises from below as it appears) did not change the INP, which stayed at 8ms to 24ms (median 16ms). INP measures only until the next frame is painted, so it ends when the first frame of the animation appears.

css
#dlg-anim[open] { animation: pop 400ms ease-out; }

A modal with the background blurred by backdrop-filter: blur(8px) took 24ms to 40ms (median 24ms), which was slightly longer. In the web-vitals breakdown, the delay until painting (presentationDelay) was 14ms to 25ms.

If the content is built before opening, that time counts toward INP

When the open button's handler built the modal content (a 5000-row list) before calling showModal(), the INP was 160ms to 216ms (median 184ms). One of the five runs was 216ms, which falls in the "needs improvement" range.

js
$('open-heavy').addEventListener('click', () => {
  $('dlg-heavy').querySelector('.body').innerHTML = rows(5000);
  $('dlg-heavy').showModal();
});

Next, I opened a modal that first showed "Loading…" and built the content afterward. Inside a requestAnimationFrame callback, I called setTimeout so that the content was built after the next frame was painted. (I omitted the timing lines from the tested code.)

js
$('open-skeleton').addEventListener('click', () => {
  const body = $('dlg-skeleton').querySelector('.body');
  body.textContent = '読み込み中…';
  $('dlg-skeleton').showModal();
  requestAnimationFrame(() => setTimeout(() => {
    body.innerHTML = rows(5000);
  }, 0));
});

With this approach, the open interaction took 16ms to 24ms (one run was under 16ms and was not recorded). Building the content later took 128ms to 175ms, but that time was not included in any interaction.

However, in these five runs, the close interaction was the one that became the INP. Closing the modal containing 5000 rows with the "OK" button took 32ms to 56ms, which was longer than the open interaction.

console
$ node measure.cjs open-skeleton 5 ok 300
open-skeleton ok n=300 run1: 開いた=true 閉じた=true 要素数=15642 interactionCount=2 あとで中身を作った時間=175ms
  INP=40 good type=pointer target=#dlg-skeleton>p>button.ok inputDelay=0 processingDuration=34 presentationDelay=6
  操作ごと(16ms以上): 開く[pointerdown,pointerup,click] duration=16 run=7 閉じる[pointerdown,pointerup,click] duration=40 run=19

The 操作ごと line is the Event Timing API entries grouped by interaction, showing only those that took 16ms or more. run is the time (in ms) that event listeners were running for that interaction. As interactionCount=2 shows, the opening click and the closing click were each counted as one interaction. When I closed the modal with the Esc key, the key press was counted as one interaction too.

On pages with many elements, showModal() opening and closing became slower

I increased the number of rows in the background list to bring the page to about 10,000 elements (10042) and then about 40,000 (40042), and measured the same "light modal" at each size. For comparison, I also included a button that opens a <dialog> with show() (not as a modal) and a modal that shows and hides a div by changing its class.

Horizontal bar chart of the median INP for opening and closing a modal, by element count. The labels in the image are in Japanese. At about 640 elements, showModal, show, and div are all 16ms. At about 10,000 elements, showModal is 48ms and the others are 16ms. At about 40,000 elements, showModal is 152ms, show is 32ms, and div is 40ms
INP by element count (median of 5 runs). Only showModal() became slower as the number of elements increased

Only showModal() became slower as the number of elements increased. Here is one run with about 40,000 elements:

console
$ node measure.cjs open-light 5 ok 20000
open-light ok n=20000 run2: 開いた=true 閉じた=true 要素数=40042 interactionCount=2
  INP=152 good type=pointer target=#dlg-light>button.ok inputDelay=0 processingDuration=126 presentationDelay=26
  操作ごと(16ms以上): 開く[pointerdown,pointerup,click] duration=96 run=66 閉じる[pointerdown,pointerup,click] duration=152 run=75

Across five runs of closing with the OK button, the open interaction took 88ms to 104ms and the close interaction took 128ms to 152ms. The close interaction was longer in every run. In both cases, run was 62ms to 88ms, meaning the time was spent inside the listener that called showModal() or close(). When I closed with the Esc key, the close interaction took 104ms to 160ms, and in two of the five runs the open interaction was longer.

On the same page, the dialog opened with show() had an open interaction of 32ms to 48ms and a close interaction (with the OK button) of 24ms to 32ms. The div modal's close interaction (with the OK button) was also 24ms to 32ms. With about 40,000 elements, even show() and the div approach took around 32ms.

MDN's explanation of showModal() says that a dialog opened with showModal() is displayed in the top layer, and that elements outside the dialog in the same document behave as if they had the inert attribute (they cannot be operated). By contrast, with show(), elements outside the dialog remain operable.

To see where the time was spent, I captured one Chromium trace each for the page with about 40,000 elements. The time for style recalculation (UpdateLayoutTree in the trace) was as follows:

How it opens Open interaction Close interaction
showModal() 69ms 71ms
show() 1ms 0ms
Toggling a div's display 0ms 0ms

Only with showModal() did style recalculation take about 70ms, both when opening and when closing. The modal content was just "Saved" and an "OK" button, so the difference comes from the number of elements outside the modal.

On a page with a poor INP, if the web-vitals attribution build reports the interaction target (interactionTarget) as a button inside the modal, such as #dlg-light>button.ok, then the slow interaction is closing the modal. However, in the Esc key tests, when the close interaction was the INP, the target was the open button outside the modal (#open-light), and the interaction type (interactionType) was reported as keyboard. So if the target is the open button and the type is keyboard, the interaction may be closing with the Esc key. Checking only the open button and finding it fast is not enough; the close interaction's time needs to be checked separately. In this test, already at about 10,000 elements, the close interaction (32ms to 56ms) was the same as or longer than the open interaction (32ms to 48ms).

​Posted by 86