Skip to content

Lesson 5 of 14 · 24m

JavaScript Bundle Budgets

A 200 KB script costs nothing like a 200 KB image — see what's in the bundle, then enforce a compressed-size budget in CI.

A 200 KB JPEG and 200 KB of JavaScript cost the same to download and wildly different amounts to use. The image decodes off the main thread and paints. The script has to be parsed, compiled, and executed — on the main thread, on a phone whose CPU is a quarter of your laptop's. Every kilobyte of JS hurts LCP if it blocks or performs the render, and hurts INP by occupying the main thread when the user first taps.

Nobody decides to ship 1.4 MB of JavaScript. It accumulates one reasonable-looking npm install at a time. The only defence that works is a budget a machine enforces.

See what you're actually shipping

# Vite / Rollup: treemap of the production build
npx vite-bundle-visualizer

# Anything that emits source maps
npx source-map-explorer 'dist/assets/*.js' --html reports/bundle.html

# webpack
npx webpack --profile --json > stats.json && npx webpack-bundle-analyzer stats.json

Day one always finds the same things: two copies of a library at different versions, a full icon set imported for six icons, moment with every locale, a charting library dragged into the initial chunk by one admin-only import, polyfills for browsers you dropped years ago.

Set the budget from the metric, not from the current size

Work backward. Target LCP ≤ 2.5 s on a mid-range phone over 4G; if HTML, CSS, and the hero image already eat most of it, the JavaScript that must execute before the page is usable gets a few hundred milliseconds of CPU at most. That lands most teams at roughly 150–200 KB compressed of critical-path JS for the initial route. Pick a number, write it down, gate on it.

{
  "scripts": { "size": "size-limit" },
  "size-limit": [
    { "name": "initial route", "path": "dist/assets/index-*.js", "limit": "170 kB" },
    { "name": "checkout chunk", "path": "dist/assets/checkout-*.js", "limit": "60 kB" }
  ]
}

size-limit measures the compressed size and fails CI when a PR crosses the line — and the diff points at the commit that did it. webpack users get a cruder built-in:

// webpack.config.js
module.exports = {
  performance: {
    hints: "error",
    maxEntrypointSize: 250000,   // bytes, uncompressed
    maxAssetSize: 250000,
  },
};

The four moves that shrink bundles

  1. Split by route and by interaction. import() the editor when the user clicks "Edit," not at boot. Route-level splitting is the floor, not the ceiling.
  2. Replace, don't trim. Intl.DateTimeFormat or date-fns instead of moment; fetch instead of an HTTP client; CSS instead of an animation library.
  3. Fix tree-shaking blockers. CommonJS packages, modules with side effects and no "sideEffects": false, and barrel files that re-export everything defeat dead-code elimination.
  4. Ship modern syntax. A browserslist still targeting dead browsers means everyone downloads transpiled generators and core-js polyfills.

Compression is not a licence

Brotli shrinks the wire bytes ~15–20% over gzip — but it does not shrink parse+execute time, which scales with uncompressed size. A 500 KB-gzipped bundle is ~1.7 MB of source the phone still has to chew through. Budget wire size because it's what you control; know the unpacked number because it's what the CPU feels.

Field notes

  • Budget the compressed size of what executes on the initial route. Total-site JS is a vanity number; lazy chunks nobody loads don't hurt anyone.
  • A budget without an owner gets raised every time it's hit. Require a named approver to bump the limit.
  • Verify content-encoding: br (or zstd) in the actual response headers, not the config you think is deployed — CDN compression toggles get lost in migrations.
  • Third-party tags from a tag manager never touch your bundler, so your budget can't see them. Track them separately — they're covered in their own lesson.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.