JS Minifier & Beautifier
Minify JavaScript by removing comments and whitespace, or beautify minified JS with indentation.
What JavaScript minification does
Minifying JavaScript removes comments and unnecessary whitespace, and - in full-featured minifiers - renames local variables to single letters. The code behaves identically and can be dramatically smaller.
JavaScript benefits from minification more than CSS does, because identifier renaming has no CSS equivalent. A descriptive local variable name appearing fifty times becomes a single character fifty times.
Key takeaways
- Minification strips comments and whitespace; full minifiers also rename local variables.
- Typical savings are 30-60% before compression.
- JavaScript is the most expensive asset on a page - it must be parsed and executed, not just painted.
- Source maps let you debug minified code as if it were the original.
- This tool performs safe whitespace and comment removal, not identifier renaming.
What a minifier does
| Operation | Before | After | Done here? |
|---|---|---|---|
| Remove comments | // explanation | (removed) | Yes |
| Remove whitespace | let x = 1; | let x=1; | Yes |
| Collapse line breaks | Multiple lines | Fewer lines | Yes |
| Rename local variables | let userCount | let a | No |
| Remove dead code | if (false) { ... } | (removed) | No |
| Inline single-use constants | const x = 5; use(x) | use(5) | No |
| Tree shaking | Unused exports | (removed) | No |
Identifier renaming and dead code elimination require full parsing and scope analysis. Use a build-time tool such as Terser or esbuild for production.
eval, with, or reflection on function names. For production builds, use a real minifier in your pipeline.Why JavaScript size matters more than other assets
A kilobyte of JavaScript is more expensive than a kilobyte of any other resource, because it is not merely downloaded - it must be parsed, compiled and executed on the main thread.
| Asset | Download | Parse | Execute | Blocks rendering? |
|---|---|---|---|---|
| Image | Yes | Decode | No | No |
| CSS | Yes | Yes | No | Yes |
| JavaScript | Yes | Yes | Yes | Yes, unless deferred |
On a mid-range phone, parsing and executing JavaScript can take several times longer than downloading it. This is why bundle size matters disproportionately on mobile.
Source maps
A source map is a file linking minified code back to the original, so browser developer tools can show you readable source, correct line numbers and original variable names while you debug production code.
- Generate them in your build, which every modern bundler does by default.
- Reference them with a
//# sourceMappingURL=comment at the end of the file. - Decide deliberately whether to deploy them publicly. Public maps make debugging easy for you and also expose your original source to anyone.
- A common compromise is uploading maps to your error tracking service without serving them from the site.
Reducing bundle size properly
- Audit your dependencies. A single convenience library can outweigh all of your own code.
- Enable tree shaking and import only what you use, not whole namespaces.
- Code-split by route so a visitor downloads only what the current page needs.
- Lazy-load anything below the fold or behind an interaction.
- Prefer modern syntax and avoid shipping transpiled code with polyfills to browsers that do not need them.
- Minify and compress as the last step, not the first.
- Measure with a bundle analyzer - the largest contributors are frequently not the ones you expect.
Beautifying minified code
Beautifying restores formatting to minified JavaScript, which is useful for reading a third-party library or investigating something in production. It restores structure, not information:
- Indentation and line breaks come back.
- Comments do not. They were removed permanently.
- Original variable names do not if the code was renamed -
a,b,cis what you get. - Module structure is generally unrecoverable after bundling.
Frequently Asked Questions
What does minifying JavaScript do?
It removes comments and unnecessary whitespace, and in full-featured minifiers renames local variables to single characters. The code behaves identically and is typically 30 to 60 percent smaller before compression.
Does this tool rename variables?
No. It performs the safe transformations - whitespace and comment removal - which cannot change behavior. Renaming requires full scope analysis and can break code using eval or reflection. Use Terser or esbuild in your build pipeline for production.
Does minification change how my code behaves?
It should not. Whitespace and comment removal is entirely safe. More aggressive transformations such as renaming and dead code elimination are safe in almost all cases, but can break code that inspects function names or uses eval.
What is a source map?
A file linking minified code back to the original source, so browser developer tools show readable code, correct line numbers and original variable names while debugging. Without one, a production stack trace pointing to line 1 column 4823 tells you almost nothing.
Should I deploy source maps publicly?
It is a trade-off. Public maps make debugging production issues straightforward and also expose your original source to anyone who looks. A common compromise is uploading maps to your error tracking service without serving them from the site.
Why does JavaScript size matter more than image size?
Because JavaScript must be parsed, compiled and executed on the main thread, not merely downloaded and decoded. On a mid-range phone, processing can take several times longer than downloading, which is why bundle size hurts mobile performance disproportionately.
What reduces bundle size more than minification?
Auditing dependencies and code splitting. A single convenience library can outweigh all your own code, and shipping every route's JavaScript to every visitor is far more costly than any amount of whitespace. Use a bundle analyzer - the biggest contributors are rarely the expected ones.
Can I recover the original code from minified JavaScript?
Only partially. Beautifying restores indentation and line breaks, but comments are gone permanently and renamed variables stay as single letters. Module structure is generally unrecoverable after bundling.