Skip to content
Learn Measure Blog Case studies About
On this page
  • How the unused JavaScript audit fails
  • How to remove unused JavaScript
    • Detect unused JavaScript
    • Build tool for support for removing unused code
  • Stack-specific guidance
    • Angular
    • Drupal
    • Joomla
    • Magento
    • React
    • Vue
    • WordPress
  • Resources

Remove unused JavaScript

Jul 7, 2020
Available in: Español, Русский, English
On this page
  • How the unused JavaScript audit fails
  • How to remove unused JavaScript
    • Detect unused JavaScript
    • Build tool for support for removing unused code
  • Stack-specific guidance
    • Angular
    • Drupal
    • Joomla
    • Magento
    • React
    • Vue
    • WordPress
  • Resources

Unused JavaScript can slow down your page load speed:

  • If the JavaScript is render-blocking, the browser must download, parse, compile, and evaluate the script before it can proceed with all of the other work that's needed for rendering the page.
  • Even if the JavaScript is asynchronous (i.e. not render-blocking), the code competes for bandwidth with other resources while it's downloading, which has significant performance implications. Sending unused code over the network is also wasteful for mobile users who don't have unlimited data plans.

How the unused JavaScript audit fails #

Lighthouse flags every JavaScript file with more than 20 kibibytes of unused code:

A screenshot of the audit.
Click a value in the URL column to open the script's source code in a new tab.
See the Lighthouse performance scoring post to learn how your page's overall performance score is calculated.

How to remove unused JavaScript #

Detect unused JavaScript #

The Coverage tab in Chrome DevTools can give you a line-by-line breakdown of unused code.

The Coverage class in Puppeteer can help you automate the process of detecting unused code and extracting used code.

Build tool for support for removing unused code #

Check out the following Tooling.Report tests to find out if your bundler supports features that make it easier to avoid or remove unused code:

  • Code Splitting
  • Unused Code Elimination
  • Unused Imported Code

Stack-specific guidance #

Angular #

If you are using Angular CLI, include source maps in your production build to inspect your bundles.

Drupal #

Consider removing unused JavaScript assets and only attach the needed Drupal libraries to the relevant page or component in a page. See the Defining a library for details.

Joomla #

Consider reducing, or switching, the number of Joomla extensions loading unused JavaScript in your page.

Magento #

Disable Magento's built-in JavaScript bundling.

React #

If you are not server-side rendering, split your JavaScript bundles with React.lazy(). Otherwise, code-split using a third-party library such as loadable-components.

Vue #

If you are not server-side rendering and using the Vue router, split the bundles by lazy loading routes.

WordPress #

Consider reducing, or switching, the number of WordPress plugins loading unused JavaScript in your page.

Resources #

  • Source code for the Remove unused code audit
  • Remove unused code
  • Adding interactivity with JavaScript
  • Code Splitting
  • Dead Code Elimination
  • Dead Imported Code
  • Find Unused JavaScript And CSS Code With The Coverage Tab In Chrome DevTools
  • class: Coverage
Last updated: Jul 7, 2020 — Improve article
Return to all articles
Share
subscribe

Contribute

  • File a bug
  • View source

Related content

  • developer.chrome.com
  • Chrome updates
  • Web Fundamentals
  • Case studies
  • Podcasts
  • Shows

Connect

  • Twitter
  • YouTube
  • Google Developers
  • Chrome
  • Firebase
  • Google Cloud Platform
  • All products
  • Terms & Privacy
  • Community Guidelines

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies.