日本語

Finding Unused CSS and JS with Chrome DevTools

​​Chrome DevTools​CSS,JavaScript

The Coverage panel in Chrome DevTools shows, for each file of CSS and JavaScript the page loaded, how many bytes and what percentage have not been used. Moving from a row in the table to the Sources panel shows the unused lines with a gray bar. This is a good starting point when you want to find code that the page loads but never uses, in order to make the page lighter. I ran it in a test environment, and the numbers and screenshots below are the actual results.

Note

The test environment was Ubuntu 24.04 with Chrome for Testing 153.0.8010.12 (the build bundled with Playwright 1.63.0). I launched Chrome headless, opened the DevTools UI that this Chrome serves in a separate Chromium tab, and operated it by connecting to that Chrome. The DevTools in this environment did not have a Japanese translation, so the UI stayed in English; the button and column names below are written in English. I did not test macOS or Windows Chrome, DevTools opened normally from a window, extension code (content scripts), or results on a public site.

The test page

I loaded one CSS file and one JavaScript file into a page that contains only a heading, a button, and a memo area that the button opens. When the button is pressed, openMemo adds the is-open class to the .memo element, and the memo is shown. The CSS for .card and .footer-nav, and the showCards function, are not used anywhere on this page.

js
function setup() {
  const button = document.getElementById('open');
  button.addEventListener('click', openMemo);
}

function openMemo() {
  const memo = document.getElementById('memo');
  if (memo.classList.contains('is-open')) {
    memo.classList.remove('is-open');
    return;
  }
  memo.classList.add('is-open');
  memo.textContent = 'メモを開きました(' + formatTime(new Date()) + ')';
}

function formatTime(date) {
  const h = String(date.getHours()).padStart(2, '0');
  const m = String(date.getMinutes()).padStart(2, '0');
  return h + ':' + m;
}

function showCards(items) {
  const list = document.createElement('div');
  for (const item of items) {
    const card = document.createElement('div');
    card.className = 'card';
    card.innerHTML = '<p class="card-title">' + item.title + '</p>';
    list.appendChild(card);
  }
  document.body.appendChild(list);
}

setup();

Open the Coverage panel and reload the page

You open the Coverage panel from the DevTools Command Menu. With DevTools open, press Ctrl+Shift+P on Windows and Linux, or Command+Shift+P on macOS, to open the Command Menu. (I confirmed the shortcuts in the Command Menu definition in the DevTools source.) Type "Show Coverage" and press Enter, and a Coverage tab opens at the bottom of the window. The screenshots in this article have the Coverage tab moved to the main tab bar at the top to make them easier to read (right-click the tab and choose "Move to main tab bar").

Right after it opens, the panel says "No coverage data" and nothing has been recorded yet. Press the second button from the left on the toolbar (Start instrumenting coverage and reload page). The page reloads and recording starts. While recording, this button changes to a red stop button. Recording starts from the beginning of the load, so code that ran when the page first opened is counted too.

On the test page, right after the reload, the table looked like this:

Chrome DevTools Coverage panel. app.js shows Total bytes 879, Unused bytes 746 (84.9%); style.css shows Total bytes 507, Unused bytes 300 (59.2%). Below the table it says "340 B of 1.4 kB (25%) used so far, 1.0 kB unused."
The Coverage panel right after reloading the page
URL Type Total bytes Unused bytes
/app.js JS (per function) 879 746(84.9%)
/style.css CSS 507 300(59.2%)

Below the table the panel said "340 B of 1.4 kB (25%) used so far, 1.0 kB unused." This means that, of the 1.4 kB of CSS and JavaScript loaded together, 340 B (25%) has been used so far. In the Usage visualization bar on the right, gray is the unused part and green is the used part.

The 746 unused bytes for JavaScript matched the sum of three functions: openMemo (267 characters), formatTime (161 characters), and showCards (318 characters). The setup function, which ran when the page opened, and the final setup(); line counted as used. openMemo and formatTime, which only run when the button is pressed, had not run yet, so they counted as unused.

Pressing the button reduces the unused amount

With recording still on, I pressed the button on the page once. The numbers in the table changed immediately.

Animation with the page on top and the Coverage panel below. The cursor moves to the button that opens the memo and clicks it. The memo appears on the page, and in the Coverage panel the Unused bytes for app.js change from 746 to 318 and for style.css from 300 to 219.
The Unused bytes numbers change after one button press
URL Unused bytes before the press Unused bytes after one press
/app.js 746(84.9%) 318(36.2%)
/style.css 300(59.2%) 219(43.2%)

Because openMemo and formatTime ran, the unused amount of JavaScript went down. The CSS also went down, because adding the is-open class made the .memo.is-open rule used. The line below the table changed to "849 B of 1.4 kB (61%) used so far, 537 B unused."

Point

"Unused" in the Coverage panel means not used since recording started. The line below the table also says "used so far." Code that is only used after an action, such as the button in this test, is counted as unused until that action happens. Before you decide that code can be deleted, try every action the page supports.

Check unused lines in the Sources panel

Double-clicking a row in the table opens that file in the Sources panel. A vertical bar appears to the right of the line numbers: used lines are green, and unused lines are gray. In style.css after one button press, lines 22 to 36, starting from .card and below, were gray.

Sources panel with style.css open. The lines up to .memo.is-open have a green bar next to the line numbers. Lines from .card at line 22 onward, including .card-title, .footer-nav, and .footer-nav a, are gray. Coverage: 56.8% is shown at the bottom right.
In style.css, everything from .card down is gray

The "Coverage: 56.8%" at the bottom right is the percentage of this file that was used. Together with the 43.2% unused in the table, it adds up to 100%.

When I opened app.js in the Sources panel too (this screenshot was opened by typing the file name after pressing Ctrl+P), showCards (lines 22 to 31), which was never called, was gray.

Sources panel with app.js open. The lines for openMemo and formatTime are green, lines 22 to 31 for showCards are gray, and line 33, setup(), is green. Coverage: 63.8% is shown at the bottom right.
In app.js, only showCards is gray

Be careful with one detail in this view: lines 9 and 10 of openMemo (inside the if, where is-open is removed and the function returns) are also green. The if condition was false on the first click, so these two lines did not run. They are green because the Type column says "JS (per function)," which means the recording is made per function. The description text for the Type column in the DevTools source says that in per-function recording, once a function runs at all, its whole body counts as used. Branches inside a function that were not taken also count as used.

Save the results as JSON

The Export coverage button on the toolbar saves the recorded result as a JSON file. In my environment it was saved as Coverage-20261113T071456.json. The file contains an array with one entry per file, and each entry has three fields: url, ranges, and text.

text
http://localhost:8080/app.js | keys: ['ranges', 'text', 'url'] | len(text): 879 | used: 561 | unused: 318
  ranges: [{'start': 0, 'end': 550}, {'start': 868, 'end': 879}]
  unused [550:868] starts with: 'function showCards(items) {', lines: 10

The output above is what a small script printed after reading the saved JSON. text is the contents of the file, and ranges are the start and end positions of the used parts. Adding up the length of each range (end minus start) gives 561. Subtracting that from the total of 879 gives 318, the same as the Unused bytes in the table. The unused range 550 to 868 was the 10 lines of showCards.

When I measured app.js as a file, it was 899 bytes in UTF-8. The 879 Total bytes in the table equals the number of characters in text. This file contains a 10-character Japanese string in the memo.textContent line, meaning "Memo opened ()", and each of those characters takes 3 bytes in UTF-8. That adds 2 bytes per character, or 20 bytes for the 10 characters. style.css contains no Japanese, so both measurements were 507. The numbers in the table may not match the file's byte size.

Before you remove the gray parts

On the test page, the .card rules and showCards shown in gray were not used anywhere on that page. However, the Coverage panel only looks at the page being recorded, and only at whether something was used while recording. If the same CSS or JavaScript is loaded on other pages, it may be used there. First, find the files with a large unused amount in the Coverage table. Next, read the gray lines in the Sources panel and check which screen or action each piece of code is for. Then decide whether to delete it, or to load it only on the pages that need it.

​Posted by 86