TapCub is live — analytics, insights and live chat, free on one platform
Analytics

Remove Unused JavaScript: Find It per Route and Check the Gain

How to act on the Lighthouse 'remove unused JavaScript' audit: measure coverage per route, remove, split or defer in a fixed order, and confirm the gain with real-user p75 data. Worked 420 KB example.

Lin Zhou Published Nov 28, 2026 14 min read Analytics
Sample data: 256 of 420 KB (61%) unused on the landing route. Removing the carousel and polyfills, lazy-loading the chart library and splitting the main bundle leaves 180 KB, of which 36 KB (20%) is unused.Sample data

To remove unused JavaScript, record coverage in Chrome DevTools on each important route, then cut in a fixed order: delete dead code, stop loading page-specific scripts site-wide, split by route, lazy-load click-only code and defer the rest. Treat the Lighthouse audit as a lead, not a verdict, and confirm the gain with real-user p75 LCP and INP.

What you will learn

  • Lighthouse flags files with over 20 KiB unused on one load; code for other routes or later clicks shows as unused too
  • Remove first, then split and lazy-load, then defer; only removal cuts download, parse and compile work entirely
  • Judge the change on field p75 LCP and INP for the same page and device mix, and watch script errors after each release

What does "remove unused JavaScript" mean?

"Remove unused JavaScript" (also shown as "Reduce unused JavaScript") is a Lighthouse and PageSpeed Insights audit. It lists script files that the browser downloaded during page load but did not execute, and Chrome's documentation says it flags every file with more than 20 KiB of unused code. The fix is not always deletion. Code can be unused because it is dead, because it belongs to another route, or because it only runs after a click, and each case has a different fix.

Unused code still costs something. The browser downloads it, parses it and compiles it, usually on the main thread that also has to respond to taps and clicks. The HTTP Archive's 2024 Web Almanac measured a median of 558 KB of JavaScript per mobile page and 613 KB per desktop page, and found a median of 206 KB of it, or 44% of bytes delivered, unused during page load on mobile.

The approach in this guide: treat the audit as a lead, not a verdict. Measure coverage per route, cut in a fixed order (remove, then split, then defer), and confirm the gain on real users rather than on one lab run. General speed fixes such as images, caching and server response time are ranked in how to speed up a website; this article covers the single JavaScript audit in depth.

Why is one Lighthouse run a lead, not a verdict?

The audit is useful, but its number describes one page load in one simulated environment. Three things it cannot tell you:

  1. Unused on load is not unused overall. Code that opens a modal, validates a form or draws a chart after a click shows as unused, because the click never happened in the lab. Deleting it breaks the feature.
  2. One URL is not the site. A single-page app often ships one bundle for every route. Code unused on the landing page may be the core of the dashboard route.
  3. Bytes saved is not time saved. Moving 80 KB off the critical path may cut Largest Contentful Paint a lot on a slow phone and not at all on a fast laptop. Only field data across your real device mix shows the effect.

So use the audit to find candidates, then answer two questions for each file: which routes need this code? and when does it need to run?

How do you find unused JavaScript per route?

Chrome DevTools has a Coverage panel built for this. The steps:

  1. Open DevTools, open the Command Menu (Ctrl+Shift+P or Cmd+Shift+P), type "coverage" and choose Show Coverage.
  2. Pick Per function or Per block in the panel's drop-down. Per block is finer and shows unused branches inside a function.
  3. Click the reload button in the panel. The page reloads while Coverage records which code runs.
  4. Do what a real visitor does on that route: scroll, open the menu, submit the form. Recording continues while you interact, so code those actions need moves from "unused" to "used".
  5. Stop recording and read the table: URL, type, total bytes, unused bytes and a usage bar. Click a row to see unused lines marked in the Sources panel.
  6. Repeat for every important route: landing page, product or pricing page, checkout or signup, and the logged-in app.

Write the results into a table with one row per script and one column per route. A script that is 90% unused on the landing page and 85% used in the app is a code-splitting candidate. A script that is unused on every route is a deletion candidate. If you have many routes, Chrome's documentation points to Puppeteer's coverage API to automate the same measurement.

Remove, split, defer or keep: which fix fits?

What the code isSignal in CoverageFixTypical saving on the route
Dead code: old features, unused library exportsUnused on every route and after interactionRemove it, or rely on tree shaking by importing only what you useFull size of the dead code
Plugin or widget not used on this page100% unused on this routeRemove from this route: load it only on pages that use itFull size on this route
Polyfills for browsers you no longer supportMostly unused in modern browsersRemove or serve only to old browsersMost of the polyfill bundle
Code another route needsUnused here, used on route BCode-split by route; load the chunk on route BChunk size on this route
Code needed after a clickUnused on load, used after interactionLazy-load on interaction (dynamic import())Moves off the critical path
Third-party script needed but not urgentPartly unused, not needed for first paintDefer (defer attribute or load after the page is interactive)Moves off the critical path, still downloaded
Framework runtime and code for the first viewMostly usedKeepNone

The order matters. Removing code is the only fix that cuts download, parse and compile work entirely. Splitting and lazy-loading move the cost to where it is needed. Deferring only changes timing. Start at the top of the table and stop when the field numbers are good.

What the code isFixSaving on the route
Dead code or unused exportsRemove or tree-shakeFull size
Plugin unused on this pageLoad only where usedFull size on this route
Legacy polyfillsRemove or serve to old browsersMost of the bundle
Code another route needsCode-split by routeChunk size
Code needed after a clickLazy-load with import()Off the critical path
Non-urgent third-party scriptDeferOff the critical path, still downloaded

Work top to bottom and stop when field metrics reach the good thresholds.

Worked example: a 420 KB landing-page bundle

Sample data: a marketing site's landing page loads five scripts totalling 420 KB (uncompressed). Coverage on the landing route, with a scroll and a click on the main menu during recording, shows 256 KB unused: 256 ÷ 420 = 61%.

ScriptSizeUnused on landingUnused shareFix
Main app bundle220 KB110 KB50%Code-split by route, landing keeps 140 KB
Chart library80 KB64 KB80%Lazy-load on the dashboard route only
Polyfills45 KB41 KB91%Remove; serve to legacy browsers only
Carousel plugin35 KB35 KB100%Remove from this route
Chat widget40 KB6 KB15%Keep, load with defer
Total420 KB256 KB61%

Two details in the table matter. First, the chart library shows 16 KB as "used" on a page with no chart, because its initialization runs on import; Coverage cannot tell useful work from setup cost. Second, the main bundle cannot lose all 110 KB of its unused code. Splitting moves 80 KB to other routes, leaving 140 KB, of which 30 KB is still unused on landing (shared utilities, error handlers).

After the changes: the landing page downloads 140 KB of main bundle plus the 40 KB deferred widget = 180 KB, of which 36 KB (20%) is unused. Removed from the landing route: 80 + 80 + 45 + 35 = 240 KB, a 57% cut. Lighthouse will still list the main bundle if its unused part exceeds 20 KiB, and that is acceptable: the last 30 KB costs more engineering time than it saves.

app.tapcub.com

Unused JavaScript on the landing page

sample, KB unused of KB loaded
Main app bundle110 of 220 KB
Chart library64 of 80 KB
Polyfills41 of 45 KB
Carousel plugin35 of 35 KB
Chat widget6 of 40 KB
Sample data
Sample data: 256 of 420 KB (61%) unused on the landing route. Removing the carousel and polyfills, lazy-loading the chart library and splitting the main bundle leaves 180 KB, of which 36 KB (20%) is unused.

In what order should you cut?

A fixed order keeps the work small and the risk low:

  1. Delete dead code and unused dependencies. Search the codebase for imports nobody calls; check whether a 70 KB library is used for one function you could write in ten lines.
  2. Stop loading page-specific plugins site-wide. Many sites include every plugin's script on every page. Attach each script to the pages that use it.
  3. Drop polyfills for browsers you no longer support, or serve them through a separate legacy bundle.
  4. Split by route, then lazy-load interaction-only code with dynamic imports.
  5. Defer third-party scripts that are not needed for the first view.
  6. Re-run Coverage on every route and run a smoke test of each feature you touched.

Ship each step separately. If field LCP improves after step 2, you know which change paid off, and a bug can be traced to one deployment.

How do you confirm the gain on real users?

Lab runs vary from run to run and use one device profile. Core Web Vitals are judged on field data: Google's guidance is to measure at the 75th percentile of page loads, segmented by mobile and desktop, with "good" thresholds of LCP ≤ 2.5 seconds, INP ≤ 200 milliseconds and CLS ≤ 0.1. See Web Vitals for what each metric measures and website performance metrics for the wider set.

Sample data, the landing page above, 14 days before and 14 days after the release:

Metric (p75, landing page)BeforeAfterChangeThreshold
Largest Contentful Paint3.1 s2.6 s−0.5 s (−16%)Good ≤ 2.5 s
Interaction to Next Paint280 ms190 ms−90 ms (−32%)Good ≤ 200 ms
Page loads sampled18,40017,900
Mobile share of loads62%61%

Read it with three rules:

  • Compare like with like. Same page, same length of period, similar device mix. A campaign that brought more desktop visitors can make p75 look better with no code change.
  • INP often moves more than LCP. Less script to parse and run means a less busy main thread when the visitor taps. Here INP crossed into "good" while LCP still sits just above 2.5 seconds, so the next fix for LCP is probably not JavaScript (check the image and server response).
  • Wait for enough samples. Percentiles on a page with a few hundred loads jump from day to day. Two weeks per side is a reasonable floor for a page with modest traffic.

What can break when you remove JavaScript?

  • Features used only after interaction. The most common breakage. Test every button, form and menu on routes you touched.
  • Code used by a small share of browsers. A polyfill removed for modern browsers may still be needed by an embedded browser inside another app.
  • Order-dependent scripts. A deferred script that another inline script expects to exist will throw an error.
  • Tag-manager and consent logic. Removing or deferring a tag can change when analytics or consent banners load; check that measurement still fires.

Watch script errors for a few days after each release. A spike right after a deploy is the fastest signal that something needed was removed.

How do you check the result in TapCub?

TapCub does not analyse your bundles or run Lighthouse; Coverage and your build tools do that. TapCub's part is the real-user check after each release, using the landing-page example above:

Page performance breakdown table with p75 LCP, CLS, INP, FCP, TTFB and samples per page
Demo data: Monitoring › Performance broken down by page. Compare the same path before and after a release.
  1. Mark the release. Add an entry in Configuration › Annotations on the day each step ships ("removed carousel and polyfills from landing"), so the trend line shows where each change landed.
  2. Read field metrics for the route. Open Monitoring › Performance, set cards to P75 and break down by page. Compare the landing page's Largest Contentful Paint and Interaction to Next Paint for 14 days before and after the annotation. The page has no previous-period comparison, so switch the date range to read each side.
  3. Check the device mix. In the same breakdown, switch the dimension to device to confirm mobile and desktop moved in the same direction, rather than a traffic-mix change producing the gain.
  4. Watch for breakage. Open Monitoring › Errors. Error rate there is script errors divided by pageviews; a new error group that first appears on the release day, on the route you changed, usually means removed code was still needed. Expand it to see the page and device.
  5. Set a guard. In All-platform analytics › Operations › Monitors, add a rule on the built-in error_rate measure and dry-run it; the dry run reports how many times it would have fired in the last 30 days, so you can pick a threshold that is quiet on normal days.

Performance metrics are reported by the visitor's browser, so pages where the script is blocked have no samples.

FAQ

How do I remove unused JavaScript?

Record coverage in Chrome DevTools for each important route while using the page normally. Then, in order: delete dead code and unused dependencies, stop loading page-specific plugins site-wide, drop unneeded polyfills, split bundles by route, lazy-load code that only runs after a click, and defer non-urgent third-party scripts. Confirm the result with field data at the 75th percentile.

Why does Lighthouse say "reduce unused JavaScript"?

Because at least one script file loaded during the test has more than 20 KiB of code that did not execute during page load. It does not mean the code is useless. It may belong to another route or run after interaction. Use the audit as a list of candidates and check each file with the Coverage panel.

Does unused JavaScript affect SEO?

Indirectly. Unused script adds download, parse and compile work, which can slow Largest Contentful Paint and Interaction to Next Paint, and Core Web Vitals are part of the page experience signals Google uses. The effect depends on how much the code delays the main thread on real devices, which is why field data matters more than the audit score.

Is it safe to remove unused JavaScript?

Removing dead code is safe; removing code that is only "unused on load" is not. Coverage marks click handlers, form validation and chart code as unused if you did not trigger them during recording. Interact with the page while recording, test every feature on routes you changed, and watch script errors after each release.

What is a good amount of unused JavaScript?

There is no official target beyond the audit's 20 KiB per-file threshold. Some unused code is normal: shared utilities and error handling rarely run on every page. In the example, cutting the landing page from 61% to 20% unused moved field INP into the good range. Stop when field metrics are good, not when the audit is empty.

Sources

Filed underAnalytics

Was this article helpful?

We use your answers to fix and expand this page.

Lin Zhou

Product Experience Specialist

Writes about product experience, practical workflows, and growth, turning complex processes into clear steps for everyday decisions.

Check speed on real visitors

P75 Core Web Vitals by page and device, script errors by release, and monitors on error rate.

See your data clearly. Find your growth.

Every click, backed by data. Install one line of code and see your first numbers in a minute.

No credit card · Free plans for analytics and chat · Cookieless analytics