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.jsonDay 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
- 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. - Replace, don't trim.
Intl.DateTimeFormatordate-fnsinstead ofmoment;fetchinstead of an HTTP client; CSS instead of an animation library. - 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. - Ship modern syntax. A browserslist still targeting dead browsers means everyone downloads transpiled generators and
core-jspolyfills.
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(orzstd) 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?
Keep reading
Related articles from the SudoSecurity blog.
What is Vite? The Build Tool That Made Frontend Development Fast Again
Vite replaced Webpack for a lot of teams without much fanfare. Native ES modules, a dev server that starts in milliseconds, and a production build that doesn't require understanding 300 lines of config. Here's how it works and why the switch is worth it.
PHP API Performance: N+1 Queries, cURL, and Connection Pools
A PHP API case study: fixing an N+1 query, twenty serial cURL calls, missing connection pooling, and stats recalculated on every request with Redis caching.
Inside TypeScript 7: What a Year-Long Rewrite in Go Actually Bought Developers
A deep look at how Microsoft's Go-based TypeScript 7 compiler achieves its speed gains, and what the Visual Studio Code team's year-long migration reveals about adopting it safely.
Node.js vs Deno vs Bun: Picking the Right JavaScript Runtime in 2026
Three JavaScript runtimes. Three different philosophies on what the developer experience should feel like. Here's how they compare on performance, ecosystem, tooling, and the only decision that actually matters for production.