JavaScript Minifier: Reduce Script Size Without a Build Tool
JavaScript minification removes everything browsers ignore — comments and whitespace — while carefully preserving string literals, template literals, and regular expressions. The result is a smaller script that runs identically to the original.
Variable names are not mangled. Use esbuild or Terser for production bundles to additionally shorten identifiers.
Tokenizer-Based Approach
A naive regex-replace minifier would corrupt strings containing // or /* characters. This tool uses a state machine that tracks whether it is inside a string literal, template literal, or comment. Whitespace and comment removal only happen in the CODE state — string contents are always passed through verbatim.
Regex Literal Detection
JavaScript's / character is ambiguous: division or start of regex. The tokenizer uses a heuristic: if the previous significant character is an operator, opening bracket, or comma, / starts a regex. This covers the vast majority of real-world regex patterns correctly.
When to Use a Bundler Instead
For production JavaScript, use esbuild (fastest), Terser, or your bundler's built-in minification. These tools mangle variable names, tree-shake unused exports, and produce source maps. This tool is best for quick snippets, inline scripts, or estimating savings before setting up a pipeline.
Practical Examples
Compressing a Utility Script for Inline Embedding
Minify a small utility to embed inside a <script> tag.
- 1.Paste the JavaScript utility with its comments and formatting
- 2.The minified output appears instantly with byte count
- 3.Verify the output — strings and logic should be intact
- 4.Copy and paste into your