日本語

Finding Slow CSS Selectors with Chrome DevTools Selector Stats

​​Chrome DevTools​CSS,INP,Performance

On a page where it takes too long from a button press until the screen changes and INP (Interaction to Next Paint, a metric based on the time from an interaction until the next frame is painted) is poor, the Performance panel may show a long Recalculate style. In that case, the Selector stats tab shows which CSS selector is taking the time. If you check "Enable CSS selector stats (slow)" before recording, a table appears with the time spent per selector and the number of elements it was checked against.

Note

The test environment was Ubuntu 24.04 with Chrome for Testing 158.0.8127.36 (the one bundled with Playwright). I launched a windowed Chrome on a virtual display and investigated with DevTools open on the right side of the window. The DevTools UI was in English, so I use the English names for buttons and columns. The page was served from a locally started HTTP server (http://localhost:8080/). I did not use any CPU throttling setting. I did not try Chrome on macOS or Windows. I did not try the Copy table feature or sorting by clicking column headers.

The test page

The page lays out 3000 product cards and makes the 100th card the "selected card." When the "Toggle display" button (its label on the page is in Japanese) is pressed, the dark class is added to body and the background turns dark gray. app.js is as follows.

js
// Lay out 3000 product cards. Make the 100th card the "selected card"
const list = document.getElementById('list');
for (let i = 1; i <= 3000; i++) {
  const li = document.createElement('li');
  li.className = i === 100 ? 'card is-selected' : 'card';
  li.innerHTML = `<div class="body"><span class="name">商品 ${i}</span><span class="price">${i * 10} 円</span></div>`;
  list.append(li);
}
// Toggle the body class with a button (this causes the whole page's style recalculation)
document.getElementById('theme').addEventListener('click', () => {
  document.body.classList.toggle('dark');
});

On the last line of style.css, I wrote a selector that fades the cards that come after the selected card. ~ selects sibling elements that follow in the same parent (MDN's explanation of the subsequent-sibling combinator).

css
body { font-family: sans-serif; margin: 16px; }
.list { list-style: none; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 4px; }
.card { border: 1px solid #ccc; padding: 4px; }
.card .name { display: block; }
.dark { background: #222; color: #eee; }
.dark .card { border-color: #555; }
/* Fade the cards that come after the selected card */
.card.is-selected ~ .card { opacity: .5; }

Check "Enable CSS selector stats" before recording

Selector stats are not recorded by default. According to the official Selector stats documentation, it is off at first because it adds recording overhead. I recorded with these steps.

  1. Open the Performance panel in DevTools.
  2. Click the gear button at the top right (Capture settings).
  3. In the opened section, check "Enable CSS selector stats (slow)."
  4. Press the Record button, press the page's toggle button, then press Stop.
The settings section opened below the Performance panel toolbar. Enable CSS selector stats (slow) is checked. A blue gear button is at the far right
Capture settings opened, with Enable CSS selector stats (slow) checked

The official documentation says to uncheck this after you finish the investigation.

Find the heaviest selector in Insights' CSS selector costs

After recording, the Insights section on the left showed INP as 149ms, and below it a "CSS selector costs" item. Expanding it shows totals for the whole recording (Total) and the selector that took the most time.

The CSS selector costs section expanded. Total: Match attempts 15063, Match count 14920, Elapsed time 71 ms. Top selector elapsed time: 70 ms, with .card.is-selected ~ .card below it. Top selector match attempt: 6000, with li below it
The Insights CSS selector costs section expanded

Under "Top selector elapsed time: 70 ms" it shows .card.is-selected ~ .card. Of the total 71 ms, 70 ms came from this single selector. The "Top selector match attempt" entry is the selector with the most match attempts, which was li (6000 times) from the browser's default styles. The selector that takes the most time and the one with the most match attempts are listed separately. The Total here is for the whole recording, so its numbers differ a little from the per-recalculation table I look at next.

See per-selector numbers in the Selector stats tab

With nothing selected in the flame chart, opening the Selector stats tab in the bottom panel showed a table for the whole recording. The official documentation describes double-clicking the CPU graph at the top to select the entire recording range. To see the table for a single Recalculate style, select that Recalculate style first and then open this tab.

To find the item you want in the flame chart, you can use Ctrl+F. I typed "Recalculate style" into the search field that appeared below and pressed Enter. Matches were selected in order, and a counter on the right showed the position, such as "1 of 3". Two of the three were shorter than 1 ms. The remaining one was the one from pressing the button. Selecting it showed "129.51 ms Recalculate style," and the Selector stats tab changed to the table for this single recalculation.

Top: the flame chart, with the selected purple Recalculate style showing 129.51 ms. Bottom: the Selector stats tab table, where the first row, (Totals for all selectors), is 71.145, and the second row, .card.is-selected ~ .card, is 70.233
Selected the Recalculate style from the button press and opened the Selector stats tab

The top five rows of the table were as follows (the Style sheet column is omitted).

text
Elapsed (ms)  Invalidation count  Match attempts  Match count  % of slow-path non-matches  Selector
71.145        3000                15028           14908        89.2                        (Totals for all selectors)
70.233        0                   3000            2900         100.0                       .card.is-selected ~ .card
0.459         0                   6000            6000         0.0                         li
0.259         3000                3000            3000         0.0                         .dark .card
0.188         0                   3000            3000         0.0                         .card

The top row is the total for all selectors. The rows below were sorted by Elapsed (ms) in descending order. The official documentation says that clicking a column header sorts the table.

The meaning of each column is as given in the official documentation table.

  • Elapsed (ms): the time spent matching that selector
  • Match attempts: the number of elements the selector was tested against
  • Match count: the number of elements the selector matched
  • % of slow-path non-matches: among the elements that did not match, the percentage that were checked by the unoptimized slow path

The Chrome version I tested also had an Invalidation count column, which is not in the official documentation table. I could not find an explanation of this column in the official documentation.

As candidates for improvement, the official documentation lists selectors with a high Elapsed (ms), a high number of Match attempts, a low Match count relative to Match attempts, and a high % of slow-path non-matches. In this table, .card.is-selected ~ .card had an Elapsed of 70.233 ms, which is more than 150 times the second row, li (0.459 ms). Of the 3000 elements tested, the 100 that did not match (the selected card and the 99 cards before it) were all checked by the slow path (100.0%).

The Summary tab for the same Recalculate style

Elapsed in Selector stats is the time spent matching selectors. The time for the whole Recalculate style can be seen in the Summary tab with the same item still selected.

The Summary tab. Recalculate style Duration 129.51 ms, Elements affected 3003, Initiated by Schedule style recalculation, Pending for 0.8 ms
Summary tab for the same Recalculate style

Duration was 129.51 ms, and the number of elements whose styles were recalculated (Elements affected) was 3003. This means one selector accounted for about 70 ms of the 129.51 ms.

Open the rule from the Style sheet column

For selectors in the page's CSS files, the Style sheet column shows a link such as "Line 8:1." Clicking "Line 8:1" on the .card.is-selected ~ .card row opened style.css in the Sources panel, and "Line 8, column 1" appeared below the editor. I could move to the place where the selector was written without searching for the file name.

The style.css file open in the Sources panel. Line 8 is .card.is-selected ~ .card { opacity: .5; }
Clicked the Line 8:1 link to open line 8 of style.css

For rows from the browser's default styles (such as li or button), the column showed "Unable to link via ua-style-sheet" instead of a link.

Rewrite the selector as a single class and record again

I stopped using the CSS ~ to find the cards after the selected card. Instead, I had JavaScript add an is-after class to those cards. I prepared the fixed version in separate files (app-fixed.js and style-fixed.css, both loaded by fixed.html). The only change from app.js is the line that decides each card's class.

js

// Add is-after via JavaScript to the cards that come after the selected card
  li.className = i === 100 ? 'card is-selected' : i > 100 ? 'card is-after' : 'card';

In style-fixed.css, I changed the last line of style.css as follows.

css
/* Fade the cards that come after the selected card (the class is added in app-fixed.js) */
.card.is-after { opacity: .5; }

I restarted Chrome and recorded with the same steps. When I selected the Recalculate style from the button press, the Selector stats tab looked like this.

Top: the flame chart, with the selected Recalculate style showing 41.22 ms. Bottom: the Selector stats table, where the first row, (Totals for all selectors), is 0.873. There is no .card.is-selected ~ .card row, and .card.is-after is 0.159
After the fix, selected the Recalculate style from the button press
text
Elapsed (ms)  Invalidation count  Match attempts  Match count  % of slow-path non-matches  Selector
0.873         3000                14928           14908        35.0                        (Totals for all selectors)
0.393         0                   6000            6000         0.0                         li
0.203         3000                3000            3000         0.0                         .dark .card
0.159         0                   2900            2900         0.0                         .card.is-after
0.111         0                   3000            3000         0.0                         .card

.card.is-after matched all 2900 elements it was tested against, and its Elapsed was 0.159 ms. Comparing before and after, the results are as follows. Both were recorded with Enable CSS selector stats (slow) checked.

Item Before After
Elapsed of the selector that fades the cards 70.233 ms 0.159 ms
Total for all selectors (Totals) 71.145 ms 0.873 ms
Duration of the Recalculate style 129.51 ms 41.22 ms
INP in Insights 149ms 60ms

In the recording after the fix, CSS selector costs did not appear near the top of the Insights section. It was included in "Passed insights (17)," a group of items with no problems.

Point

After fixing the selector found with Selector stats, record the same operation again and check whether Elapsed (ms) and the Recalculate style time went down. The official documentation also recommends this check.

For the steps to record in the Performance panel itself and how to find long tasks, see my article on finding slow processes with the Performance panel.

​Posted by 86