How to Reduce Unused JavaScript
Find which scripts carry unused code and cut it: code splitting, loading features on demand, lighter libraries, and fewer plugin and third-party scripts.
"Reduce unused JavaScript" is one of the most common warnings in Lighthouse and PageSpeed Insights. It means the page downloads JavaScript that never runs while the page loads. That code still costs download time, parse time and memory, and on a mid-range phone it delays both Largest Contentful Paint and Interaction to Next Paint.
This guide shows how to find which scripts carry the unused code, how to decide what to cut, and the fixes for the most common sources: bundles, plugins, libraries and third-party tags.
What "unused JavaScript" actually means
Lighthouse loads the page, records which bytes of each script were executed, and reports the rest as unused. It lists each script with its transfer size and the estimated wasted bytes, and only flags files where the waste is large enough to matter.
Two things to keep in mind:
- Unused during load is not the same as useless. Code for a modal, a checkout step or a menu that only opens on click will show as unused. The goal is not to delete it, but to stop loading it before it is needed.
- The number is an estimate for one page. A shared bundle may be 70% unused on the home page and fully used on the product page. Check the pages that matter most, on mobile.
Why it slows the page down
JavaScript is the most expensive kind of byte on the web. An image of the same size only has to be decoded; a script has to be downloaded, parsed, compiled and often executed on the main thread. While the main thread is busy, the browser cannot respond to taps or paint new content. That is why heavy bundles show up as long tasks, high Total Blocking Time in the lab, and slow INP for real visitors.
Large scripts also compete for bandwidth with the things visitors actually see first: the HTML, CSS, fonts and hero image.
Step 1: Find the scripts with the most waste
- Run the page through our JavaScript performance analyzer or a full Site Vitals audit, and open the list of recommendations. Note the scripts with the largest wasted bytes.
- In Chrome DevTools, press
Ctrl+Shift+P(Cmd+Shift+Pon Mac), type Show Coverage, and reload the page from the Coverage panel. Each file shows total bytes and unused bytes, and clicking a file highlights the unused lines in red. - Interact with the page (open the menu, scroll, open a modal) and watch the unused percentage fall. What stays red after normal use is the code you can most safely remove or defer.
- Group the results by owner: your own bundle, a framework, a plugin or app, or a third party. Each group has a different fix.
Step 2: Fix your own bundles
Split code by route
If every page loads one big app.js, each page pays for every other page. Modern bundlers (Vite, webpack, Rollup, esbuild) split code automatically when you use dynamic imports. Load each page's component with import() so a visitor on the blog never downloads the dashboard.
// Loaded only when this route is visited
const Dashboard = () => import('./pages/Dashboard.js');
Load features on interaction
Code for things that open later, such as a date picker, a chat window, a video player or a rich text editor, can be imported when the visitor first reaches for it:
button.addEventListener('click', async () => {
const { openEditor } = await import('./editor.js');
openEditor();
});
For code that is needed soon but not immediately, start the import when the browser is idle or when the element scrolls into view, so it is ready by the time the visitor clicks.
Check what your libraries add
- Run a bundle analyzer (for example
rollup-plugin-visualizerorwebpack-bundle-analyzer) to see which packages take the most space. - Import only what you use:
import debounce from 'lodash/debounce'instead of the whole library, and icon packages that support per-icon imports. - Replace heavy libraries where the platform now does the job: native
fetchinstead of an HTTP client,Intl.DateTimeFormatinstead of a large date library for simple formatting, CSS for animations that used to need JavaScript. - Make sure tree shaking works: use ES module builds, avoid side-effect imports, and build in production mode.
Stop shipping legacy code to modern browsers
Polyfills and transpiled code for very old browsers add weight for everyone. Set a realistic browser target (for example via a browserslist config) so the bundler stops converting modern syntax that every current browser already understands.
Step 3: Fix plugins, apps and themes
On WordPress, Shopify and similar platforms, most unused JavaScript comes from plugins and apps that load their scripts on every page, even when only one page uses them.
- Remove what you do not use. Deactivated is not enough on some platforms: uninstalled apps can leave snippets in the theme. Search the theme for leftover script tags.
- Load per page. A contact form plugin only needs its script on the contact page. Many performance plugins let you disable scripts per page or per post type.
- Prefer lighter alternatives. A slider that ships a 100 KB library to rotate three images is rarely worth it; a static hero image is usually faster and converts just as well.
Step 4: Tame third-party scripts
Analytics, tag managers, chat widgets, A/B testing tools, ad scripts and social embeds often account for most of the unused code, and you cannot edit them. You can control when and whether they load:
- Audit the tag manager and remove tags nobody uses any more.
- Load non-essential tags after the page is interactive, or on first interaction.
- Replace embeds (YouTube, maps, social posts) with a lightweight placeholder that loads the real embed on click.
- Load chat widgets when the visitor clicks a "Chat" button instead of on every page view.
Our guide to how third-party scripts affect website performance covers this in more depth.
Step 5: Make the remaining JavaScript cheaper
- Load scripts with
defer(ortype="module", which is deferred by default) so they never block the first render. - Serve scripts compressed with Brotli or gzip and cache them long-term with hashed file names. See browser caching explained.
- Break up long tasks: if a script must run a lot of work at startup, split it into chunks and yield to the main thread between them.
What not to do
- Do not delete code just because Coverage shows it as red. Error handlers, rarely used features and code for other pages also show as unused. Test the whole site after each change.
- Do not chase 0%. Some unused code is normal. Aim to remove the big, obvious chunks: whole libraries, whole plugins and page-specific code loaded everywhere.
- Do not combine everything into one file to "save requests". With HTTP/2 and HTTP/3, several smaller, cacheable files are usually better than one giant bundle.
How to check the result
Re-run the same page on mobile and compare the JavaScript transfer size, Total Blocking Time and LCP. Lab results vary between runs, so compare a few runs rather than one. Then watch real-user INP over the following weeks; our guide on monitoring Core Web Vitals over time explains how.
Ready to see where your JavaScript goes? Run a free scan with the JavaScript performance analyzer.