Find Unused JavaScript And CSS Code With The Coverage Tab In Chrome DevTools

The Coverage tab in Chrome DevTools can help you find unused JavaScript and CSS code. Removing unused code can speed up your page load and save your mobile users cellular data.

Analyzing code coverage.
Figure 1. Analyzing code coverage.

Overview

Shipping unused JavaScript or CSS is a common problem in web development. For example, suppose that you want to use Bootstrap's button component on your page. To use the button component you need to add a link to Bootstrap's stylesheet in your HTML, like this:

...
<head>
  ...
  <link rel="stylesheet"
        href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
        crossorigin="anonymous">
  ...
</head>
...

This stylesheet doesn't just include the code for the button component. It contains the CSS for all of Bootstrap's components. But you're not using any of the other Bootstrap components. So your page is downloading a bunch of CSS that it doesn't need. This extra CSS is a problem for the following reasons:

  • The extra code slows down your page load. See Render-Blocking CSS.
  • If a user accesses the page on a mobile device, the extra code uses up their cellular data.

Open the Coverage tab

  1. Open the Command Menu.
  2. Start typing coverage, select the Show Coverage command, and then press Enter to run the command. The Coverage tab opens in the Drawer.
The Coverage tab.
Figure 2. The Coverage tab.

Record code coverage

  1. Click one of the following buttons in the Coverage tab:
    • Click Start Instrumenting Coverage And Reload Page Start Instrumenting Coverage And Reload Page if you want to see what code is needed to load the page.
    • Click Instrument Coverage Instrument Coverage if you want to see what code is used after interacting with the page.
  2. Click Stop Instrumeting Coverage And Show Results Stop Instrumeting Coverage And Show Results when you want to stop recording code coverage.

Analyze code coverage

The table in the Coverage tab shows you what resources were analyzed, and how much code is used within each resource. Click a row to open that resource in the Sources panel and see a line-by-line breakdown of used code and unused code.

A code coverage report.
Figure 3. A code coverage report.
  • The URL column is the URL of the resource that was analyzed.
  • The Type column says whether the resource contains CSS, JavaScript, or both.
  • The Total Bytes column is the total size of the resource in bytes.
  • The Unused Bytes column is the number of bytes that weren't used.
  • The last, unnamed column is a visualization of the Total Bytes and Unused Bytes columns. The red section of the bar is unused bytes. The green section is used bytes.

Feedback

Was this page helpful?
Yes
What was the best thing about this page?
It helped me complete my goal(s)
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
It had the information I needed
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
It had accurate information
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
It was easy to read
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
Something else
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
No
What was the worst thing about this page?
It didn't help me complete my goal(s)
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
It was missing information I needed
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
It had inaccurate information
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
It was hard to read
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
Something else
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.