日本語

Making INP Worse with :has() in Core Web Vitals

​​Core Web Vitals​Chrome,CSS,Playwright,web-vitals

CSS :has() re-evaluates its condition on every click, so I wondered whether it makes Core Web Vitals INP (Interaction to Next Paint) worse. For a list with a checkbox on each row, where checking a row changes its background or shows a "delete selected" toolbar like a mail app's bulk delete, I measured INP for each :has() selector style.

On a page with about 100,000 elements (20,000 rows), .row:has(input:checked), a row-scoped :has(), gave an INP of 16ms, the same as a page with no :has(). The largest INP came from a style where one checkbox changes every row, body:has(input:checked) .row, at 184ms. Setting the same look with a class on body from JavaScript also gave 184ms. Showing the toolbar at the top of the page, which pushes the list down, gave 128ms with :has() and 104ms with a class.

Note

The test environment was Ubuntu 24.04 (4 CPU cores), Node.js 22, and web-vitals 6.2.3. I used Chromium 158, bundled with Playwright 1.63.0, in headless mode with a 412px by 823px viewport. The page was served from a local server on localhost:8080. I ran each measurement three times and report the median unless noted otherwise. I did not test CPU throttling, real smartphones, browsers other than Chrome, the Chrome DevTools UI, or real-user data (PageSpeed Insights field data).

Comparing nine selector styles on a checkbox list page

The test page was a list of memos with checkboxes. Each row was an li.row containing a label, a checkbox, and two span elements, five elements in total. The ?n= URL parameter set the row count to 1,000, 5,000, or 20,000. At 20,000 rows the page had 100,012 elements. At the top of the page was a "delete selected" toolbar (.toolbar), hidden by default with display: none.

js
li.innerHTML = `<label><input type="checkbox" id="c${i}"><span class="title">メモ ${i + 1}</span><span class="meta">2026-11-${String(i % 28 + 1).padStart(2, '0')}</span></label>`;

The ?css= URL parameter selected a single CSS variant for each page. I prepared both :has() styles and, for comparison, versions that set the same look with a class added from JavaScript.

js
const CSS = {
  none:         ``,                                                   // no :has()
  row:          `.row:has(input:checked) { background: #eefafd; }`,
  rowClass:     `.row.checked { background: #eefafd; }`,             // class on the row via JS
  bodyToolbar:  `body:has(input:checked) .toolbar { display: block; }`,
  toolbarClass: `body.selecting .toolbar { display: block; }`,       // class on body via JS
  toolbarFixed: `.toolbar { position: fixed; top: auto; bottom: 0; left: 0; right: 0; }
                 body:has(input:checked) .toolbar { display: block; }`,
  bodyRows:     `body:has(input:checked) .row { color: #1f2a30; }`,
  bodyClass:    `body.selecting .row { color: #1f2a30; }`,           // class on body via JS
  sibling:      `.row:has(~ .row input:checked) { background: #eefafd; }`,
};

sibling changes the background of rows above the checked row. The ~ inside :has() looks at sibling elements that come after it.

For the class-based styles, a change listener toggles a class on the row or on body. For body, the code keeps a count of checked boxes in a variable and toggles the class without searching the DOM.

js
let checked = 0;
list.addEventListener('change', (e) => {
  checked += e.target.checked ? 1 : -1;
  document.body.classList.toggle('selecting', checked > 0);
});

For the measurement, Playwright clicked the checkbox on the fifth row five times, 300ms apart, alternating between checking and unchecking. INP was collected with the attribution build of onINP from web-vitals, and I used the last value the page reported. I passed reportAllChanges: true and durationThreshold: 16 to onINP. With reportAllChanges, a value is reported each time INP changes. According to the README, the default durationThreshold is 40ms; I lowered it to compare smaller values. I also recorded how much time style recalculation took (RecalcStyleDuration) with Performance.getMetrics from the Chrome DevTools Protocol.

INP on the 20,000-row page

Horizontal bar chart comparing INP across nine selector styles for five checkbox clicks. No :has(), .row:has(input:checked), and the row class via JS all show 16ms. The sibling :has() shows 32ms. body:has() toolbar shows 72ms when overlaid at the bottom and 128ms when it pushes the list down. The body class toolbar via JS shows 104ms. body:has() recoloring all row text and the body class doing the same both show 184ms, just under the 200ms line.
INP on the 20,000-row page (100,012 elements), web-vitals onINP, median of three runs. Labels in the image are in Japanese.
Selector style INP (median of 3) Range over 3 runs Style recalculation (total for 5 clicks)
No :has() 16ms 16–32ms 0ms
.row:has(input:checked) 16ms 16ms 1–2ms
Class on row (JS) 16ms 16–24ms 7–9ms
.row:has(~ .row input:checked) 32ms 24–32ms 50–54ms
body:has() toolbar (overlaid at bottom) 72ms 56–72ms 51–57ms
body:has() toolbar (pushes the list down) 128ms 88–128ms 45–55ms
Class on body toolbar (JS) 104ms 96–136ms 31–35ms
body:has() recoloring all row text 184ms 176–200ms 604–653ms
Class on body recoloring all row text (JS) 184ms 144–224ms 479–640ms

According to web.dev's INP guide, 200ms or less is "good" and more than 500ms is "poor." Every style had a median at or below 200ms. In one of the three runs, the class on body that recolors all row text reached 224ms, which is "needs improvement."

According to MDN's PerformanceEventTiming guide, interaction durations are rounded to 8ms steps, and the recording threshold (durationThreshold) cannot go below 16ms. The 16ms values in the table are the smallest this method can report, so smaller differences cannot be told apart.

On the 1,000-row page (5,012 elements), the median INP was 16ms for all nine styles. On the 5,000-row page (25,012 elements), the all-row text recoloring was 56ms with :has() and 48ms with a class, and the push-down toolbar was 32ms with :has() and 48ms with a class. The rest were 16ms.

A row-scoped :has() barely changed INP

The INP for .row:has(input:checked) was 16ms, the same as the page without :has(). Style recalculation totaled 1–2ms over five clicks.

I used Chrome traces to check which elements were recalculated. On the first click, which checks a box, the style recalculation (UpdateLayoutTree in the trace) recalculated 3 elements in 0.2ms. It did not recalculate the whole list, only those three elements.

With the sibling form .row:has(~ .row input:checked), 7 elements were recalculated. INP was 32ms, a bit higher than without :has(), but still far below 200ms.

Recoloring every row is slow with :has() and with a class alike

The INP increase came from body:has(input:checked) .row, which changes the text color of every row. On the trace for the first click, style recalculation recalculated 40,001 elements and took 105.3ms.

When I set the same text color with a class on body from JavaScript, the number of recalculated elements was the same 40,001, and the time was 87.4ms. The median INP was 184ms for both. Across the five clicks, total style recalculation was 604–653ms for :has() and 479–640ms for the class. The ranges overlap.

In this measurement, what raised INP was not whether :has() was used. It was the number of elements whose style was recalculated (all rows' text color) and the number of rows pushed down (the toolbar, covered in the next section). MDN's :has() guide also recommends not using ancestors with many descendants, such as body or :root, as the subject of A:has(B), where A is the subject. In this test, the all-row recoloring with body as the subject produced the high INP. A style that changes only one toolbar, with body as the subject, recalculated just 3 elements.

Point

If one checkbox needs to change every row's look, replacing :has() with a class did not change INP. To lower INP, reduce the number of elements that change. For example, change only the checked row with .row:has(input:checked). If the toolbar needs to show the selection, overlay it without pushing the list down, as described in the next section.

Overlaying the toolbar on the list cut 128ms to 72ms

The body:has(input:checked) .toolbar toolbar recalculated only 3 elements, yet its INP was 128ms.

Sorting the long tasks in the trace, the longest was the pre-paint work (PrePaint in the trace), at 54.6ms. Without :has(), the page showed 3.2ms. The toolbar appears at the top of the page, so the 20,000 rows below it are pushed down. With the class on body from JavaScript showing the toolbar, PrePaint was 55.7ms and the median INP was 104ms.

So I kept the same :has() and changed the toolbar to position: fixed at the bottom of the screen, overlaid. The list is not pushed down.

css
.toolbar { position: fixed; top: auto; bottom: 0; left: 0; right: 0; }
body:has(input:checked) .toolbar { display: block; }

top: auto cancels the top: 0 from the original position: sticky; top: 0 on .toolbar.

PrePaint dropped to 17.0ms, and the median INP went from 128ms to 72ms. It did not get down to the 16ms of the page without :has().

Key points of the measurement script

The number of elements recalculated during style recalculation was counted by collecting a Chrome trace with Playwright's browser.startTracing() and summing elementCount from UpdateLayoutTree events.

js
await browser.startTracing(page, { categories: ['devtools.timeline', 'disabled-by-default-devtools.timeline'] });
await page.locator('#c4').click();
await page.waitForTimeout(300);
const ev = JSON.parse((await browser.stopTracing()).toString()).traceEvents;
const ult = ev.filter((e) => e.name === 'UpdateLayoutTree' && e.ph === 'X');
const count = ult.reduce((a, e) => a + (e.args?.elementCount || 0), 0);

The actual script only aggregates records after 50ms before each click. If you are unsure whether to add :has() to your own page, comparing this count before and after adding it shows how many elements a single checkbox causes to be recalculated.

​Posted by 86