JavaScript Minifier

Minify JavaScript by stripping comments and whitespace. Regex-based; preserves strings, regex literals.

0 bytes
0 bytes

Share on Social Media:

Why minify JavaScript

JS files are typically the largest static asset on a webpage. A 200KB authored JS file might minify to 80KB — a 60% reduction. Combined with gzip on the wire, the actual transferred bytes drop further. Modern build tools (Vite, Rollup, esbuild) minify automatically using AST-based minifiers like terser. This tool is a fast regex-based minifier for one-off use, learning, or build-free workflows where pulling in a build chain isn't worth it.

What this minifier does

A character-level scan of the source that preserves syntactically-significant content while stripping insignificant content:

  • Removes single-line comments (// ...) and multi-line comments (/* ... */).
  • Preserves string literals (single, double, and template literals). Comments and whitespace inside strings stay.
  • Preserves regex literals via heuristic detection (a / after (=,;:!&|?[{ or whitespace = start of regex; a / after a word/value = division).
  • Collapses whitespace between tokens. Newlines between statements are preserved (so the result still parses).
  • Removes space around operators ({ } ( ) ; , : = + - * / % & | < > ! ?) where the surrounding tokens make the space unnecessary.

What it CAN'T do (use a real minifier for these)

Regex-based minification has limits. For deeper optimization, use terser, uglify-js, or esbuild:

  • Variable renaming. Real minifiers rename function calculateUserAge to function a, saving 80%+ on variable names. Regex can't safely do this.
  • Dead code elimination. Removing unreachable code requires AST analysis.
  • Constant folding. 1 + 2 + 3 becoming 6.
  • Tree shaking. Dropping unused imports across modules.

Our regex minifier captures 30-50% savings — comments and whitespace. Real AST minifiers go to 60-80%.

When to use this tool

Embedded scripts. Minify a small inline <script> tag without launching a build chain.

Email templates. Some email clients accept JS in templates; size matters.

Bookmarklets. Browser bookmarklets need to be a single line; minify before encoding.

Gist sharing. Sharing a code snippet where the recipient just needs the runnable result.

Quick experiments. Test minification effect before integrating a real toolchain.

Use real tools for production code

For production deployment, terser (Node.js), uglify-js (JS), or esbuild (Go-based, very fast) are the right tools. They handle edge cases this regex minifier doesn't — async/await syntax, decorators, JSX, optional chaining. Use this tool for quick experimentation and small inline scripts; use a real toolchain for app bundles.

Privacy

Minification runs in your browser. Your JS never reaches our server. Safe for proprietary internal code.