JavaScript is the most expensive resource on most pages. Beyond download time, every kilobyte has to be parsed, compiled and executed on the main thread, which hurts INP and often LCP, and the cost is several times higher on mid-range phones than on developer laptops.
1. Measure what you ship
Use a bundle analyzer (for example webpack-bundle-analyzer, rollup-plugin-visualizer or source-map-explorer) to see which modules make up each bundle. Look for large dependencies, duplicates (two versions of the same library) and code that only one page needs but every page loads.
2. Split by route
Load only the code a page needs. Most frameworks split by route automatically; check that heavy features (editors, charts, maps) live in their own chunks.
// load a heavy component only when it is needed
const ChartPanel = lazy(() => import('./ChartPanel'));
3. Lazy-load below-the-fold and on interaction
Widgets that are not visible at first, or only used on click (chat, video players, comment sections), can load later: on scroll into view or on first interaction.
4. Replace heavy dependencies
| Instead of | Consider |
|---|---|
| A full date library with all locales | Native Intl APIs, or a lighter library importing only what you use |
| A full utility library | Native array and object methods, or per-function imports |
| A large charting library for one chart | A lighter library or a server-rendered image |
| Polyfills for every browser | Polyfills only for the browsers you support |
5. Make tree shaking work
Use ES module imports, avoid importing whole packages (import { debounce } from 'lib' rather than import lib from 'lib'), check that dependencies declare sideEffects: false where true, and avoid patterns that defeat tree shaking, such as barrel files that re-export everything.
6. Ship modern JavaScript
If you no longer support very old browsers, target modern syntax so the build does not include transpiled helpers and polyfills that modern browsers do not need.
7. Compress and cache
Serve Brotli (or gzip) compression, long cache lifetimes with content hashes in file names, and HTTP/2 or HTTP/3. See HTTP caching headers.
8. Render on the server
Server rendering or static generation puts content in the HTML so the page is useful before JavaScript runs, and lets you hydrate only the interactive parts.
9. Audit third-party scripts
Tag managers, analytics, A/B testing, chat and ad scripts often outweigh your own code. See third-party scripts and performance.
10. Set a budget
A performance budget in CI stops the bundle growing back.
Worked example
A dashboard's main bundle was 1.1 MB (310 KB compressed). The analyzer showed a date library with all locales (230 KB), two versions of a UI library, and a rich-text editor loaded on every page. The team switched to per-locale imports, deduplicated the UI library, and lazy-loaded the editor on the one page that used it. The main bundle fell to 140 KB compressed; real-user INP p75 on mobile improved from 310 ms to 190 ms.
Measure the effect
Compare real-user INP and LCP per page before and after, on mobile especially, and Total Blocking Time in lab tests. VitalRix real user monitoring shows Web Vitals per page and device so you can confirm the change helped real visitors. Related: Total Blocking Time and React performance monitoring.
Common mistakes
- Measuring only on a fast laptop: test on a mid-range Android phone or with CPU throttling.
- Splitting too finely, creating dozens of tiny requests.
- Lazy-loading what is needed for the first screen, which delays LCP.
- Optimising your code while third-party scripts dominate.
Measure on a realistic device
Developer laptops parse JavaScript several times faster than mid-range phones. Use Chrome devtools CPU throttling (4× or 6×) or a real mid-range Android device when you measure parse and execution time, and compare real-user INP on mobile, which is what your visitors and search engines see. The why is my website slow guide walks through the full diagnosis, and Next.js performance monitoring covers framework-specific bundle issues.
Keep it small over time
Add a bundle-size check to pull requests that comments the size difference and fails above a limit, review new dependencies for size before adding them, and look at the bundle analyzer once a quarter. Bundles grow by default; only a habit keeps them small.
How big should a JavaScript bundle be?
As small as the page allows. Many teams aim for under 150 to 200 KB compressed for mobile-critical pages.
Does compression solve bundle size?
It reduces download time, but the browser still parses and executes the uncompressed code. Smaller is better.
What is the quickest win?
Usually lazy-loading heavy features that most visitors never use, and removing duplicate or oversized dependencies.
Try VitalRix free
Uptime monitoring, APM, logs, real user monitoring and status pages in one place. No credit card needed.