Less JavaScript, Better Performance: How to Audit What You Ship

The fastest JavaScript is the JavaScript you do not send. Audit every script by asking whether the page needs it, whether it needs it at load, and whether HTML or CSS can do the job instead. Remove what is unused, replace what the platform already provides, and load the rest only on the pages and at the moments it is needed.

Tanvirul Islam Tusher at his desk beside a list of five performance practices: reduce JavaScript, optimize CSS, modern images, self-hosted fonts and the right stack
Graphic from my LinkedIn post. The dashboard scores and timings are illustrative, not measurements of this website.

In modern web development, adding more code rarely makes a website better. Often the most effective optimization is removing what the page does not need. This post expands a LinkedIn post of mine into a practical audit, starting with the biggest lever, JavaScript, and then covering CSS, images, fonts and architecture more briefly.

The examples come from this site. It ships no JavaScript files at all: the home page carries 503 bytes of inline script for the light and dark theme, and a blog post about 1.1 KB, which adds the copy-link button. That was a series of decisions, and most of them can be made on any site.

Why does JavaScript cost more than its file size?

An image is downloaded and decoded. A script is downloaded, then parsed, compiled and executed, and that work runs on the browser’s main thread, the same thread that responds to taps and clicks. While a long script is running, the page cannot react.

That is why heavy JavaScript shows up in two places. It can delay the first render when scripts block it, which hurts Largest Contentful Paint. And it can make the page slow to respond after it has loaded, which is what Interaction to Next Paint measures. The cost is highest on the devices many visitors actually use: mid-range phones with slower processors than a developer’s laptop.

So 100 KB of JavaScript is not equivalent to 100 KB of images. Bytes of script are the most expensive bytes on the page.

How do you audit the JavaScript a page loads?

Start with facts rather than assumptions. Four steps cover most sites:

  1. List what loads. Open Chrome DevTools, go to the Network tab, reload, and filter by JS. Note each file, its size and its domain. Third-party domains are often the surprise.
  2. Measure what is unused. The Coverage panel (open the command menu with Ctrl+Shift+P and type “coverage”) shows how much of each file actually ran during the page load. A bundle that is 70% unused on the home page is a strong candidate for splitting.
  3. Look inside your bundle. A bundle analyzer for your build tool shows which libraries take up the space. One date library or icon set imported whole can outweigh the code you wrote.
  4. Question every script. For each one, ask four things: does this page need it, does it need it immediately, does every visitor need it, and could HTML or CSS do the same job?

The fourth step is where most of the savings come from. Removing a script outright beats compressing it.

What can HTML and CSS do instead?

Many common interface patterns no longer need JavaScript, because the browser provides them:

Pattern Built-in replacement
Accordion, FAQ, show more <details> and <summary>
Modal dialog <dialog>
Menus, tooltips, simple popups The popover attribute
Lazy-loading images and iframes loading="lazy"
Carousel snapping CSS scroll snap
Sticky headers position: sticky
Smooth scrolling to anchors scroll-behavior: smooth
Date and number formatting Intl.DateTimeFormat, Intl.NumberFormat

On this site, every FAQ, on the service pages and on the about page, is a <details> element. They open and close, work with the keyboard, are announced correctly by screen readers, and run no script. The answers are also in the HTML from the start, which matters for search engines and for anyone reading with JavaScript off.

What about third-party scripts?

These are the scripts teams forget they added: chat widgets, analytics and tag managers, social embeds, A/B testing, consent banners, review widgets. Each one is code you do not control, often loading more code of its own.

For each one, ask whether it still earns its place. Then consider a lighter version:

  • Share buttons can be plain links. This site’s blog has LinkedIn, Facebook and X share buttons. Each network offers an embeddable widget, but those load third-party JavaScript and can set tracking cookies on every visit. Each network also accepts a simple share URL, so the buttons here are ordinary links that cost nothing until someone clicks.
  • Embeds can use a facade. A video embed can show a thumbnail and load the real player only when the visitor clicks play.
  • Chat widgets can load on intent. Load the widget when someone clicks “Chat with us”, not on every page view.
  • Analytics can be lighter. Some sites run several overlapping tools; one is usually enough.

How do you load only what is needed, when it is needed?

For the JavaScript that does belong on the page, timing matters as much as size:

  • Do not block rendering. Load scripts with defer or type="module" so HTML parsing continues while they download. Use async only for independent scripts that do not depend on the page or on each other.
  • Split by route. A visitor reading the home page should not download the code for the dashboard. Most modern bundlers split code by route automatically when you use dynamic import().
  • Load features on demand. A rich text editor, a map or a chart library can load when the user opens the part of the page that uses it.
  • Hydrate only what is interactive. Frameworks built around islands, such as Astro, render the page as HTML and attach JavaScript only to the components that need it, sometimes only when they scroll into view.

What about CSS, images and fonts?

The same principle applies to everything else on the page: ship what the page needs, in the form it needs.

CSS. Remove unused styles and avoid one stylesheet for the whole site if each page uses a small part of it. If your CSS is small, inlining it removes a render-blocking request; this site inlines about 7 KB per page, and the case study on reaching 100/100 explains when that trade-off makes sense.

Images. Use WebP or AVIF, serve sizes that match the layout, give every image dimensions, and lazy-load only images below the first screen. Never lazy-load the main image a visitor sees first. What to fix first for Core Web Vitals covers this in more detail.

Fonts. Self-host them, subset them to the characters you use, and preload only the font that renders your largest text. On this site that is the heading font; the other two load normally.

Architecture. Static generation, server rendering and client rendering each have their place. A marketing site or blog rarely needs to build its pages in the visitor’s browser; a dashboard full of live data might. Choose per page, based on how often the content changes and how interactive it is.

Should you chase a perfect score?

No. The goal is the best experience with the least unnecessary overhead, not a number.

Here is a recent example from this site. A page speed scanner gave the home page 2 out of 3 points for weight: 414 KB, against its own target of 350 KB. Most of that weight is three variable fonts. I tested trimming the fonts to only the weights the site uses, which saved 66 KB. Then I compared every page before and after, and found that the trimmed fonts slightly changed the width of some letters. On phones, a few paragraphs wrapped differently and looked worse. I reverted the change. The page stays at 414 KB, still a light page, and the text renders exactly as designed.

A tool’s threshold is a prompt to investigate, not an instruction. Measure the trade-off, and keep the change only if visitors are better off.

A checklist for your next audit

  1. Filter the Network tab by JS and list every script and its domain.
  2. Use the Coverage panel to find code that never runs on the page.
  3. Remove scripts nobody can justify, starting with third parties.
  4. Replace scripted patterns with <details>, <dialog>, popover or CSS where you can.
  5. Load the remaining scripts with defer or as modules, split by route, and load optional features on demand.
  6. Test on a mid-range phone, or with CPU throttling in DevTools, not only on your own computer.
  7. Re-measure, and keep only the changes that improve the experience.

Common questions

Is JavaScript bad for SEO?

No. Google can render JavaScript. But content that depends on scripts to appear is slower to process and easier to break, and heavy scripts can hurt Core Web Vitals. Important content and links are safest in the initial HTML.

How much JavaScript is too much?

There is no fixed number, because the cost depends on what the code does and the devices running it. A better test is whether every script has a reason to be on that page, and whether the page responds quickly on a mid-range phone.

Should I remove my analytics or chat widget?

Not necessarily. Keep the tools that earn their cost, and load them in the lightest way available: deferred, on demand, or through a lighter alternative. Remove the ones nobody looks at.

Does a faster site rank higher?

Speed is one part of Google’s page experience signals, but it does not outweigh relevance. A fast page that does not answer the search will not rank because it is fast. The clearer benefit is to visitors, who leave slow pages before they finish loading.

If your site carries more than it needs

Auditing scripts, removing what is unused and changing how the rest loads is part of the technical SEO work I do, implemented rather than handed over as a report. For SMM panels, where themes often bundle large libraries for small effects, it is part of panel customization.

Send me a URL and I will tell you which scripts it could do without.