Paj Digital Labs
All articles
Web Design & Engineering

JavaScript Performance Budgets: Stop Shipping Code Nobody Uses

A small compressed bundle can still monopolise a cheap phone. Budget download, execution and interaction separately, then enforce the budget on the journeys that matter.

Paj Digital Labs 4 min read
Web design workspace with desktop and laptop displays
Editorial illustration · Web Design & Engineering

Bundle size is the performance number teams can most easily put in a pull request. That makes it useful, but it also makes it tempting to mistake for the whole problem. A script has to download, decompress, parse, execute and sometimes hydrate a large part of the page before it lets the visitor work.

A cheap phone can download your bundle quickly and still spend uncomfortable time executing it. Set a budget for the experience, not just the archive.

Pick a representative journey

Choose a real entry page and a primary action: opening an article, searching a catalogue or completing the first checkout step. Measure a production build on a representative lower-end device or a clearly documented lab profile.

Record initial JavaScript transfer, total executed JavaScript, long tasks and interaction responsiveness. Field INP can show how the real population experiences interactions, while lab traces help explain a specific slow action. Neither replaces the other.

Do not compare an empty development page against a production page full of content. Development tooling, sourcemaps and unoptimised code make that comparison misleading in both directions.

Separate initial and deferred code

| Budget | What it controls | |---|---| | Initial route transfer | Code required to reach the first useful screen | | Deferred feature transfer | Cost paid when a feature is actually used | | Main-thread work | Parsing, execution and rendering pressure | | Interaction latency | Delay experienced during a specific action |

Write illustrative starting limits based on your baseline and audience rather than borrowing a universal number. A technical editor and a text-only article page have different requirements. Your target should be concrete enough to fail a check and realistic enough that the team does not routinely waive it.

A useful first milestone is reducing an observed regression. If an article route loads a chart library it never renders, remove that dependency before arguing about the perfect global limit.

Inspect the dependency graph

Use your bundler's analysis output to find large modules and which imports pulled them into the route. A shared barrel can accidentally expose a whole feature to every page. Look for editors, date libraries, icon collections, chart engines and duplicated dependencies.

Tree shaking depends on package structure and side effects. Importing one symbol from a package is not a guarantee that only one symbol ships. Check the generated output rather than deciding from the elegance of the source line.

Consider whether the dependency solves a real domain problem. Removing a proven parser to hand-roll a broken smaller parser is not a performance win. Remove unused capability and load necessary capability at the right time.

Split by a meaningful boundary

A heavy editor used only in management screens should not be in the public article bundle. A chart opened on demand can be loaded after the action. Keep the initial experience complete enough that the visitor is not waiting on a chain of avoidable imports.

js
async function openOptionalChart(container, data) {
  container.setAttribute(class="text-code-string">"aria-busy", class="text-code-string">"true");
  try {
    const { renderChart } = await import(class="text-code-string">"./optional-chart.js");
    renderChart(container, data);
  } catch {
    container.textContent = class="text-code-string">"The chart could not be loaded.";
  } finally {
    container.removeAttribute(class="text-code-string">"aria-busy");
  }
}

This is a browser-side example. Server-rendered frameworks also evaluate module imports, so browser-only libraries need the framework's supported client boundary and dynamic-loading pattern. A rendering guard alone does not prevent a module from executing during server import.

Do not split every small function into a separate network request. Too many dependent chunks can create a waterfall. Inspect the actual request sequence on a cold visit.

Prefetch with restraint

Preloading something the visitor is likely to use can reduce wait time, but it still consumes bandwidth and may create execution work. An article page should not eagerly fetch every management-screen bundle because they share a navigation shell.

Measure route prefetching behaviour on slower connections and consider data-saving preferences where available. Treat speculative work as a budget item. A faster second page is not free if the first page downloaded ten routes that nobody opened.

Cache hashed assets with an appropriate long-lived policy, and make deployment retain chunks needed by already-open sessions. A visitor clicking a deferred feature after a release should not hit a missing chunk just because the previous build was removed immediately.

Enforce a check that matches the budget

A simple CI check can start with a generated manifest for the initial route. Include its transitive static imports, not merely the entry file. Measure the compression method you actually serve, and be explicit if a local check approximates it.

Store the measured baseline alongside the limit and make exceptions temporary. A comment saying "required for launch" without an expiry is not a budget; it is a historical excuse.

Track execution separately with a repeatable performance run. Record medians across multiple runs and investigate substantial changes. Lab timings are noisy, so avoid failing every pull request over a tiny fluctuation.

Watch real interactions

Open a long article, expand navigation, search and copy a code example. Look for long tasks tied to event handlers, large synchronous filtering operations or avoidable rerenders. Moving a calculation to a worker can help some workloads, but it does not repair a huge DOM or a layout thrash.

After release, compare field results for the affected route and device group. A bundle reduction that does not improve the user journey is still worth understanding. Perhaps images dominate, the server is slow, or a third-party script owns the main-thread delay.

A performance budget works when it changes decisions early. It should tell you what the visitor must pay for, what can wait and which regression needs a reason. The goal is not the smallest application. It is the least work necessary to deliver the experience well.

Paj Digital Labs

The engineering journal of Paj Digital Solutions — hosting, server infrastructure and web engineering, written by the people who run the servers.

Related reading