To reduce JavaScript on a website, first measure what loads and what actually runs, then work through it in order of impact: remove scripts nobody needs, delay the ones that are not needed straight away, replace JavaScript features with plain HTML and CSS where the browser can now do the job, and split what is left so each page only loads its own code.
JavaScript is the most expensive thing a page can download. An image of the same size only needs to be decoded and drawn. JavaScript has to be downloaded, parsed, compiled and executed on the main thread, which is the same thread that responds to taps and clicks. On a mid-range phone, that cost is several times higher than on the laptop you test with. That is why "Reduce unused JavaScript" is one of the most common warnings in PageSpeed Insights and why heavy scripts are behind most poor Interaction to Next Paint scores.
This guide works for WordPress, Shopify, Next.js and hand-built sites. The tools differ, the order does not.
Step 1: See what JavaScript you are shipping
Open the page in Chrome, open DevTools, and use three views:
- Network tab, filtered to JS. Shows every script, its size and where it comes from. Sort by size. Note which domains are not yours.
- Coverage tab (open the command menu with Ctrl+Shift+P or Cmd+Shift+P and type "coverage"). Reload the page and it shows how much of each file was actually used. A script that is 90 percent unused on the home page is a candidate for removal or splitting.
- Performance panel. Record a page load with CPU throttling and look for long tasks, marked in red. Click them to see which script caused them.
PageSpeed Insights also lists the largest unused scripts under "Reduce unused JavaScript" and the scripts that take the most time under "Reduce JavaScript execution time".
Write down every script with three notes: what it does, who asked for it, and which pages need it. This list is the plan.
Step 2: Remove what nobody needs
On most business sites I audit, this step does the most good for the least effort. Typical finds:
- Analytics or ad pixels from a campaign that ended long ago.
- Two analytics tools doing the same job, or the same tool loaded twice.
- A chat widget that nobody answers.
- Plugins or apps that were uninstalled but left scripts in the theme.
- Heatmap and session recording tools left running after the test was finished.
- Social share widgets that load on every page but are rarely used.
- jQuery loaded only for one small effect that CSS could do.
Ask the owner of each script whether it is still used. If nobody can say what it is for, remove it on a staging copy and see if anything breaks.
Step 3: Delay what is not needed at first
Some scripts are needed, just not during the first seconds. The browser gives you a few ways to load them later.
| Technique | When to use |
|---|---|
defer attribute | Your own scripts that need the HTML but not before the page is shown |
async attribute | Independent scripts like some analytics, where order does not matter |
| Load on interaction | Chat widgets, video players, maps: load when someone clicks |
| Load when visible | Below-the-fold widgets like review carousels |
| Load after page load | Low-priority trackers |
A simple "load on click" pattern for a chat widget looks like this:
<button id="chat-btn" type="button">Chat with us</button>
<script>
document.getElementById('chat-btn').addEventListener('click', () => {
const s = document.createElement('script');
s.src = 'https://chat.example.com/widget.js';
s.onload = () => window.ChatWidget && window.ChatWidget.open();
document.head.appendChild(s);
}, { once: true });
</script>The visitor sees a normal button. The heavy widget only downloads if someone actually wants to chat.
On WordPress, many caching and optimisation plugins offer "delay JavaScript until user interaction". It can work well, but test menus, forms, sliders and cookie banners after enabling it, because some scripts break when delayed.
Step 4: Replace JavaScript with HTML and CSS
Browsers can now do many things that used to need a script. Every one you switch removes code from the page.
- Accordions and FAQs: the
<details>and<summary>elements open and close without any JavaScript. - Modal dialogs: the
<dialog>element handles focus and the backdrop. - Simple popovers and menus: the
popoverattribute shows and hides an element with a button. - Carousels: CSS scroll snap creates swipeable rows. Many sliders are unnecessary anyway.
- Smooth scrolling:
scroll-behavior: smoothin CSS. - Animations on hover or focus: CSS transitions.
- Sticky headers:
position: sticky. - Lazy-loading images and iframes: the
loading="lazy"attribute.
A JavaScript-free FAQ item:
<details>
<summary>Do you offer weekend appointments?</summary>
<p>Yes, on Saturdays from 9am to 1pm.</p>
</details>It is accessible, works with the keyboard, and costs nothing to run.
Step 5: Split and trim the code you keep
For sites built with a JavaScript framework or a build tool, the goal is that each page only loads what it uses.
- Code splitting. Load heavy components with a dynamic
import()only when needed, such as a booking calendar on the booking page. - Smaller libraries. Replace large utility or date libraries with smaller ones, or with built-in browser features like
Intl.DateTimeFormat. - Import only what you use. Import individual functions rather than whole libraries.
- Drop old polyfills. If your build still targets very old browsers, it may ship code modern browsers do not need.
In Next.js with the App Router, the biggest lever is keeping components as server components and adding "use client" only to small interactive pieces. A server component's own code is not sent to the browser. I explain how I applied this in how I built a website that scores 100 on Lighthouse.
Platform-specific tips
WordPress. Page builders, sliders and "all-in-one" plugins load scripts site-wide. Use an asset manager to unload plugin scripts on pages that do not use them, and choose lightweight themes. With Elementor, turn on its performance experiments and avoid widgets that pull in extra libraries. The Elementor speed list has specific settings.
Shopify. Each app can inject scripts into every page, sometimes even after it is uninstalled. Check the theme code for leftovers, and prefer theme features over apps where you can.
Tag managers. A tag manager makes adding scripts easy, which is exactly why they pile up. Review the container every few months and remove tags nobody can explain.
How to check you made progress
Compare before and after on the same pages:
- Total JavaScript transferred in the Network tab.
- Total Blocking Time in Lighthouse.
- Long tasks in the Performance panel with CPU throttling on.
- INP in your field data over the following weeks. If interactions were slow, the guide to fixing INP shows how to trace them.
Set a budget once you are happy. For example: no more than a set amount of JavaScript on the home page, and no new third-party script without a reason written down.
The short version
Measure first with the Network, Coverage and Performance tools. Remove scripts nobody uses, delay the ones that can wait, replace JavaScript with HTML and CSS where browsers now do the job, and split the rest so each page loads only its own code. Then set a budget so it does not creep back.
If your site is slow to respond and you want someone to trace every script, that is a core part of my speed optimization service. Send me the URL and I will show you what is running.