Compress JavaScript
Compress and minify JavaScript code online. Remove comments, whitespace, and blank lines to reduce file size instantly in your browser.
About
Unminified JavaScript inflates page load time. Every unnecessary byte - comments, whitespace, blank lines - costs latency. A 200KB bundle with verbose formatting can often shrink by 30 - 60% through safe comment and whitespace stripping alone. This tool performs real, string-aware minification directly in your browser. It tokenizes your source to distinguish code from string literals and regex patterns, ensuring content inside quotes or template literals remains untouched. No server round-trip. No data leaves your machine.
Note: this tool performs safe compression (comment removal, whitespace collapsing, blank line elimination). It does not rename variables or perform dead-code elimination - those require a full AST parser like Terser or Closure Compiler. For production builds, pair this with a bundler. For quick ad-hoc minification of scripts, config files, or snippets, this handles the job without installing tooling. Results degrade gracefully on malformed input; syntax errors in source will appear in output unchanged.
Formulas
The compression ratio quantifies how much smaller the output is relative to the input:
Where R = compression ratio (percentage saved), Soriginal = byte size of the input source, and Scompressed = byte size of the minified output. Byte size is computed via new Blob([str]).size to account for multi-byte UTF-8 characters accurately.
The tokenizer operates as a finite state machine cycling through states:
At each character ci, transitions are evaluated in priority order. Escape sequences (\) skip the next character. When in CODE state and encountering //, the machine enters LINE_COMMENT and discards until newline. For /*, it enters BLOCK_COMMENT and discards until */. Quote characters toggle the corresponding string states. Regex detection uses lookbehind heuristics: a / is treated as regex start if preceded by an operator, keyword, or opening bracket.
Reference Data
| Technique | Description | Typical Savings | Risk Level |
|---|---|---|---|
| Single-line comment removal | Strips // comment outside strings | 5 - 15% | None |
| Multi-line comment removal | Strips /* ... */ blocks outside strings | 5 - 20% | None |
| Leading/trailing whitespace | Removes indentation and trailing spaces per line | 10 - 25% | None |
| Blank line removal | Eliminates empty lines between statements | 3 - 10% | None |
| Multiple space collapsing | Reduces consecutive spaces to single space | 2 - 5% | None |
| Newline collapsing | Joins statements onto fewer lines where safe | 5 - 15% | Low |
| Semicolon normalization | Ensures semicolons before newline removal | 0 - 2% | Low |
| Variable mangling | Renames local vars to short names (a, b, c) | 10 - 30% | High (requires AST) |
| Dead code elimination | Removes unreachable branches | 0 - 20% | High (requires AST) |
| Property mangling | Shortens object property names | 5 - 15% | Very High |
| Gzip compression (server) | Transfer encoding, not code transform | 60 - 80% | None (orthogonal) |
| Brotli compression (server) | Better ratio than Gzip for text assets | 65 - 85% | None (orthogonal) |
| Tree shaking | Removes unused ES module exports | 10 - 50% | Medium (bundler) |
| Scope hoisting | Flattens module wrappers | 2 - 5% | Low (bundler) |
| Console.log removal | Strips debug logging statements | 1 - 5% | Low |