In this article · 9 sections
- Step 1: Find what's unused
- Step 2: Remove what you don't need
- Step 3: Load scripts only where needed
- Step 4: Defer and delay
- Step 5: Code splitting (for apps)
- Step 6: Replace heavy features with lighter ones
- Step 7: Minify and compress
- Measure the impact
- FAQs
- Can I remove all unused JavaScript?
- Will removing scripts break my site?
PageSpeed Insights often shows "Reduce unused JavaScript" with a list of large scripts. Every kilobyte of JavaScript must be downloaded, parsed and executed, which is slow on budget phones. Here's how to reduce unused JavaScript without breaking your site.
Step 1: Find what's unused
- PageSpeed Insights: lists scripts with estimated unused bytes
- Chrome DevTools → Coverage tab (Ctrl+Shift+P → "Show Coverage"): reload the page and see how much of each file runs
- Network tab: sort by size to find the heaviest scripts
Step 2: Remove what you don't need
Common culprits: - Old libraries (jQuery plugins, sliders, animation libraries) no longer used - Page builder scripts loaded on every page - Plugins for features you removed - Duplicate libraries (two versions of jQuery, multiple icon libraries) - Forgotten tracking tags from old campaigns
Deleting is the biggest win.
Step 3: Load scripts only where needed
- Load the booking calendar script only on the booking page
- Load gallery/lightbox only on gallery pages
- In WordPress, use asset management plugins or code to dequeue scripts on pages that don't need them
Step 4: Defer and delay
<script src="/js/main.js" defer></script>
deferfor your own scripts- Delay non-essential third-party scripts until user interaction (chat, social widgets). See third-party scripts
Step 5: Code splitting (for apps)
Bundlers (Vite, webpack) can split code so each page loads only its part. Dynamic import() loads features when needed.
Step 6: Replace heavy features with lighter ones
- CSS instead of JavaScript for simple animations and accordions (
<details>element) - Native lazy loading instead of lazy-load libraries
- Simple WhatsApp link instead of a heavy chat widget
- Lightweight icon SVGs instead of icon fonts. See SVG for web design
Step 7: Minify and compress
Minification and Brotli/GZIP compression reduce transfer size. See HTTP/2, HTTP/3 and Brotli.
Measure the impact
Re-run PageSpeed Insights and watch Total Blocking Time and INP improve.
FAQs
Can I remove all unused JavaScript?
Not entirely. Some code runs only after interactions. Aim to remove large, clearly unnecessary chunks.
Will removing scripts break my site?
It can, so test on a staging copy first. See staging sites.
Joboo Web audits and trims JavaScript as part of speed optimisation. Ask us.