Developer and Text Tools

How to Analyze JavaScript Bundle Size with a Treemap and Source Maps

Find what makes a JavaScript bundle heavy using webpack stats, esbuild metafiles or source maps: a treemap, package sizes, duplicates and a budget.

Saizul Amin
Saizul Amin2 Nov 2026 · 4 min read
How to Analyze JavaScript Bundle Size with a Treemap and Source Maps

How to Analyze JavaScript Bundle Size with a Treemap and Source Maps

A small web app that ships a megabyte of JavaScript is common, and the cause is rarely your own code. It is usually a handful of modules: a charting library imported whole, a date library with every locale, two copies of the same utility package. Large bundles take longer to download, parse and execute, which hurts most on mid-range phones and slow connections. This guide explains how to see exactly where the bytes come from and what to do about it.

Where bundle size information comes from

Bundlers already know which module contributed which bytes. You just have to ask them to write it down.

BundlerHow to get data
webpackwebpack --json > stats.json
esbuildbuild with --metafile=meta.json
Vite or Rolluprollup-plugin-visualizer with the raw-data template
any bundlerthe .js.map source map

How source maps help

A source map, defined by the ECMA-426 standard (tc39.es/ecma426), connects positions in the generated file back to original source files. Its mappings field is a compact string of Base64 VLQ numbers describing segments. With only the map, you can see the original source sizes from its sourcesContent, which is useful but not what users download. If you also have the generated .js file, every segment can be measured in generated bytes, which gives the real shipped size per original file plus any code that maps to nothing, such as the bundler's own runtime.

Uncompressed versus transfer size

Most bundle tools report raw, uncompressed bytes. Servers usually send JavaScript compressed with gzip or Brotli, which is much smaller. Raw size still matters, because the browser parses and compiles the uncompressed code, but keep the difference in mind when comparing with network panels.

Analyzing a bundle with the tool

Open the JavaScript Bundle Analyzer and press Open stats or source map. For a source map, select the .js file at the same time to get exact generated sizes.

  • Treemap. The whole bundle is drawn as rectangles whose area is proportional to size, laid out with the squarified algorithm so boxes stay close to square and easy to compare. Hover for path, size and share; click a folder or package to zoom in; use the breadcrumb to go back. Each node_modules package appears as one box.
  • Packages. A table ranks dependencies by size, separates your own code from third-party code, and adds tips for libraries known to be heavy.
  • Duplicates. Packages found in more than one node_modules folder are listed with the bytes you could save, counted as the size of every copy except the largest.
  • Modules and budget. Search every module, set a size budget in KB to see whether you are over or under, and export the module list as CSV.

A worked example

The sample bundle weighs 689,700 bytes (673.5 KB) across 17 modules. The treemap's largest box is chart.js at 205,300 bytes, followed by react-dom at 131,900. The Duplicates view shows lodash twice: once at the top level and once nested under another widget package, wasting 72,500 bytes. The project's own code is 52,900 bytes, under 8 % of the total. With a budget of 250 KB, the analyzer reports the bundle as over by roughly 423 KB.

From that picture the next steps are clear. Register only the chart types you use instead of the full chart library, align the dependency versions so lodash is included once, and consider loading the chart page lazily so it is not part of the first download.

Common fixes

  • Import narrowly. Pull in specific functions or modules rather than an entire library's entry point, if the library supports it.
  • Deduplicate. Align version ranges, run your package manager's dedupe command, or force one version with overrides or resolutions.
  • Split code. Load heavy, rarely used features with dynamic import() so they leave the initial bundle.
  • Check locales and polyfills. Date and internationalisation libraries often bring every language unless configured otherwise.
  • Set a budget and track it. Run the analysis after major dependency changes and compare exports over time.

Mistakes to avoid

  • Judging a library by its npm page alone. What matters is what ends up in your bundle after tree shaking.
  • Reading original source sizes from a map without the .js file and treating them as shipped bytes.
  • Optimising your own components first, when one dependency is several times larger than all of them together.

All files are read in your browser, so stats files and source maps from private projects are not uploaded.

Limits

Sizes are uncompressed bytes; gzip or Brotli sizes appear only when the input already contains them, as visualizer data can. The analyzer cannot see code fetched at runtime from other servers or loaded by third-party scripts. Very large stats files can take a few seconds to read. Tree shaking and minification are decided by your bundler, so the tool reports the result but cannot change it, and it does not measure runtime performance; use real browser profiling for that.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages