Stop sending JavaScript your page downloads but never runs
Reduce unused JavaScript is the Lighthouse and PageSpeed Insights audit that lists every script carrying more than 20 KiB of code the page didn't run while it loaded, measured at the script's compressed size. It kept its name in Lighthouse 13, as a diagnostic, while many of the audits around it became insights. The fix is to stop sending that code up front: drop libraries you no longer use, import only the parts you need and load the rest when a visitor reaches the feature that needs it.
What the unused JavaScript table shows
URL
A script whose unused code comes to more than 20 KiB at its compressed transfer size. The Coverage panel shows uncompressed bytes, so its numbers run higher. Files that waste less than that aren't listed, however many of them there are.
Transfer Size
How big the script was over the network, after compression.
Est Savings
The part of that transfer taken up by code the load never ran, which is roughly what you'd save by not sending it on this page.
Rows under a bundle
When a bundle ships with a source map, Lighthouse lists up to five of the source files inside it with the most unused bytes, so you can see which module or package the waste comes from.
The 20 KiB limit, its measure at compressed size and the per-module rows come from the audit's source code. Chrome's post on moving Lighthouse to insights says audits outside its rename tables were left as they were.
Why unused on load doesn't mean never used
Lighthouse loads the page once and nobody clicks anything, so code for menus, forms, dialogs and checkout counts as unused even though visitors need it later. The aim isn't to delete that code but to keep it from loading before anyone asks for it. Deleting dead code and deferring the rest both bring the number down.
Unused bytes still cost time. A script that blocks rendering has to be downloaded, parsed, compiled and run before the page can draw, and even an async script competes for bandwidth with the files the first view needs, as Chrome's guide explains. web.dev's guide to removing unused code adds that it can slow Largest Contentful Paint and Interaction to Next Paint.
Load a feature's code the first time it's used
// Before: the chart code loads on every page view
// import { renderChart } from "./chart.js";
// After: it loads when someone opens the report
reportButton.addEventListener("click", async () => {
const { renderChart } = await import("./chart.js");
renderChart(reportData);
});
How to reduce unused JavaScript
- Record coverage in DevToolsOpen the Coverage panel from the DevTools command menu, reload and read the Unused Bytes column for each script. Keep recording while you open menus and submit forms, so code they run stops counting as unused.
- Find which package the waste comes fromExpand a bundle's row in Lighthouse if your build ships source maps, or open your bundler's bundle analyzer. A whole library imported for one function shows up as one large block.
- Remove or narrow the importDelete libraries the site no longer uses. Where a library allows it, import the single module you need instead of the package's main entry point.
- Split the rest by route and featureTurn on code splitting so each page loads only its own code, and load dialogs, editors and widgets with a dynamic import when someone opens them, as in the example above.
- Review third-party tagsAnalytics, chat and ad scripts often sit near the top of the list. Remove the tags nobody uses any more and load the others once the main content is on screen.
Frequently asked questions
Why does Lighthouse flag code my page needs?
Because it only watches the page load. Code that runs after a click, a scroll or a form submit looks unused to Lighthouse. That code still shouldn't load up front: split it out and fetch it when the visitor reaches the feature, as web.dev's code splitting guide shows with dynamic imports.
Does minifying fix the unused JavaScript warning?
Only partly. Minifying shrinks every byte, used or not, so the savings figure drops, but the unused code still downloads and still gets parsed. Remove or split the code first, then minify what's left.
Does fixing it raise my Lighthouse performance score?
Not directly. Chrome's scoring guide says only the metrics feed the score, not the audits and diagnostics. Sending less JavaScript usually lowers Total Blocking Time and can bring Largest Contentful Paint forward, and those metrics do count.
Cut script weight and the time it takes to run
- Website speedFind what makes your pages slow enough that visitors leave.
- JavaScript execution timeRank the scripts by the time they spend being parsed and run, then cut or delay the costliest.
- Unused CSSFind the stylesheets carrying rules a page never uses, then split them by page or strip the dead rules.
- Page sizeReduce download weight and page complexity by removing code and content you no longer need.