Developer Tools

JavaScript Minification and Source Maps Explained

How JavaScript minification removes whitespace, mangles names and drops dead code, plus how source maps keep production debugging practical.

3 min read·Updated 2026-07-26

Shipping readable source straight to production hands over your structure to every visitor and slows the download. JavaScript minification compresses scripts for delivery while source maps restore debuggability afterwards. This guide walks through each stage of the process, what each stage is allowed to change, and when you should skip minification entirely.

Stage one: whitespace removal

The safest step strips characters the engine ignores anyway: newlines, indentation, comments and the semicolons that automatic semicolon insertion already supplies. Nothing about behaviour changes; only bytes do. On chatty, well-commented source this alone commonly saves 15–25%.

Stage two: name mangling

Mangling renames local variables, parameters and private fields to one or two characters, because the engine never sees those names at runtime:

function calculateTotal(items) { var sum = 0; ... } →
function n(t){var r=0;...}

Scope-aware manglers will not touch globals, exported functions or property keys unless you explicitly allow it — renaming an exported API is a breaking change, not an optimisation.

Stage three: dead-code elimination

Modern tools fold constants, drop unreachable branches and remove exports nothing imports. This is the same machinery behind tree shaking:

TechniqueWhat it removesRequirement
Constant foldingconst BASE = 2 * 100 evaluated onceValue must be statically known
Dead branch removalif (false) { ... }Condition must be constant
Tree shakingUnreferenced exports from librariesES modules, side-effect-free code
Pure annotationUtility calls the bundler may drop/*#__PURE__*/ hints

CommonJS require() defeats most of this — migrating to import statements is often a bigger win than any minifier setting.

Source maps keep production debuggable

A minified stack trace showing at n (app.4f2a.js:1:8421) is useless on its own. A source map is a sidecar file that records the original position of every token, ending with:

//# sourceMappingURL=app.4f2a.js.map

Browsers load it only when DevTools opens, so visitors never pay for it. Best practice: publish the map, serve it over HTTPS, and accept that determined users can reconstruct your source — or withhold maps from production if the code is sensitive, at the cost of harder incident debugging. Keep unminified builds or CI artifacts either way so you can symbolicate crash reports later.

When not to minify

  • Local development — readable errors and breakpoints matter more than bytes.
  • Content Security Policy without a bundler — inline minified code is hard to audit.
  • Code whose behaviour depends on Function.prototype.toString() — rare, but mangling breaks it.
  • Debug builds on real devices — attach a source map instead of shipping raw source.
  • Tiny inline snippets — the overhead of the pipeline outweighs a few hundred bytes.

To inspect what a minifier did to your file, reverse the process with the Code Beautifier (HTML, CSS, JS), which restores indentation and line breaks for reading. Produce delivery files with the JavaScript Minifier.

Frequently asked questions

Does minification change how my code runs? A correct minifier preserves semantics, including hoisting and closure behaviour. Problems usually come from relying on function names or on undocumented globals.

Are minified files safe from theft? No. Minification is an optimisation, not protection. Obfuscation is a separate, weaker discipline.

Should libraries be minified too? Ship both: an unminified file for debugging and a .min.js for production, each with its own source map.

Tools used in this guide

Related guides