Reduce JavaScript: How to Make Your Website Lighter and Faster
Too much JavaScript slows loading and makes pages feel sticky. Audit what your site ships, remove unused code and plugins, and defer or delay what remains.
JavaScript is the most expensive kind of file you can put on a page. Unlike an image, which is simply decoded and drawn, a script must be downloaded, parsed, compiled and executed, usually on the same main thread that also handles everything the visitor does. On a fast laptop that is barely noticeable. On a mid-range phone it can add seconds.
The fastest script is the one you do not send. This guide shows how to find out what your site loads and trim it.
Four steps to a lighter site
Step 1: find out what you ship
- Open Chrome DevTools and the Network tab, filter by JS, and reload. Sort by size.
- Use the Coverage tool (in DevTools, under More tools) to see how much of each script actually runs on the page. Large percentages of unused code are common.
- Run PageSpeed Insights and read the diagnostics "Reduce unused JavaScript" and "Reduce JavaScript execution time".
- Group scripts by owner: your theme, plugins, analytics, advertising, chat, fonts and embeds.
Most owners are surprised by how many scripts come from third parties they forgot about.
Step 2: remove what you do not need
Every script should justify its place. Ask for each one: who uses this, and what would break if it were gone? Typical removals include:
- Old analytics or tracking tags from tools you stopped using.
- Social share counters and feeds that nobody looks at.
- Sliders, animation libraries and effects plugins on pages that would work as static content.
- Duplicate libraries, for example two versions of jQuery loaded by different plugins.
- Widgets running site-wide that are only needed on one page.
On WordPress, deactivate plugins one at a time on a staging copy and compare results. Plugins that load assets on every page when they are used on one are common offenders; many allow disabling that per page, or a performance plugin can unload scripts selectively.
Step 3: change how remaining scripts load
defer and async
A normal script tag blocks HTML parsing while it downloads and runs. Two attributes change that:
deferdownloads the script in parallel and runs it after the HTML is parsed, in order. This is the right choice for most of your own scripts.asyncdownloads in parallel and runs as soon as ready, in no guaranteed order. It suits independent scripts such as analytics.
Delay until interaction
Non-essential scripts, such as chat widgets and some marketing tags, can wait until the visitor scrolls, taps or a few seconds pass. Many caching and performance plugins offer this option. Test carefully, since delaying a script that something else depends on causes bugs.
Load only where needed
A contact form script belongs on the contact page, a map script on the location page. Conditional loading is one of the most effective and least risky changes.
Step 4: for developers
- Code splitting: divide bundles by route so each page loads only its own code.
- Tree shaking: use ES modules and bundlers that drop unused exports.
- Lighter dependencies: replace large libraries when you use only a small part of them.
- Break up long tasks: keep work in chunks under about 50 milliseconds, yielding to the browser between them.
- Minify and compress: serve JavaScript minified and compressed with gzip or Brotli, ideally through a CDN.
- Modern syntax: avoid transpiling and polyfilling for browsers you do not need to support.
Third-party scripts need special care
Scripts from other domains add DNS lookups, connections and code you do not control. A tag manager makes adding them easy, which is exactly why they pile up. Review the tag manager container every few months, remove stale tags and give every remaining tag an owner. Also consider the legal side: analytics and marketing tags should only run after consent where the law requires it, and waiting for consent has the side effect of keeping them out of the initial load.
How to measure improvement
| Check | What to look for |
|---|---|
| Total JavaScript transferred | Fewer kilobytes in the Network tab |
| Unused code in Coverage | Lower unused percentage |
| Total Blocking Time (lab) | Lower; a useful lab proxy for responsiveness |
| INP (field) | Interactions under 200 ms for most visitors |
Keep it from growing back
JavaScript tends to creep. Set a rule that every new plugin or tag is tested before and after for page weight and speed, and review quarterly. Document what each script does and who asked for it, so that removing it later is a quick decision, not an investigation.
A realistic example on WordPress
Imagine a small business site with a page builder, a slider plugin, a contact form plugin, a pop-up plugin, an analytics tag, a chat widget and a social feed. Each looks harmless alone, but together they might load a dozen scripts on every page, including the blog posts that need none of them.
- List every script on one blog post and identify its plugin.
- Remove the social feed and the pop-up plugin if they do not bring measurable value.
- Configure the contact form plugin to load its assets only on the contact page.
- Delay the chat widget until the visitor interacts.
- Defer the remaining theme scripts.
That sequence is typical, takes an afternoon, and often removes a large share of the JavaScript without any visible change for the visitor. Always make a backup first and test the main journeys afterwards: menus, forms, search and checkout.
A solid base for a lean site
A lighter site also needs less from the server. Zebra Hosting Cloud shared hosting is a sensible home for a lean brochure site, with Plesk to manage PHP versions and free SSL included.