JavaScript Minifier
Remove comments, collapse whitespace, and minify JavaScript.
JavaScript Minifier strips comments and collapses whitespace from JS with a deliberate, lightweight approach — the lib header says it plainly: "Basic minification only (no AST-based optimization)". Line comments are removed with a lookbehind that protects URLs (https:// stays intact), block comments go, keyword boundaries for return/typeof/instanceof and friends are preserved so operators can be squeezed without merging identifiers, and trailing semicolons before } are dropped.
Paste into "JavaScript Input", click "Minify", and read "Minified Output" next to Original / Minified / Saved size cards (Blob byte counts). Copy or download as minified.js; "Clear" resets both panes. The transform runs on click — not per keystroke — and never leaves the browser, making it a quick size sanity check rather than a full build-tool replacement.
How it works
minifyJs in lib/jsMinifier.ts removes // comments via a lookbehind/lookahead pair (?<!:)\/\/(?!\/) so :// in URLs is skipped, then strips /* */ blocks, trims lines, collapses spaces, and guards keyword boundaries by appending a space after typeof|instanceof|in|of|new|delete|void|return|throw|case|yield before re-splitting merged identifiers. Spaces around operators {}();,=:<>!&|?+\-*/% are squeezed, ;} loses its semicolon, and newlines are removed.
compareSizes uses Blob byte sizes for the Original/Minified/Saved cards. State lives in jsMinifierSlice; the Minify button guards empty input with toast "Please enter JavaScript to minify" and otherwise toasts "JavaScript minified!". Download writes minified.js as a local Blob — no network, no terser/uglify dependency.
How to use
- Paste JS into "JavaScript Input" (placeholder "Paste your JavaScript here...").
- Click "Minify" (empty → toast "Please enter JavaScript to minify").
- Read "Minified Output" (placeholder "Minified result will appear here...").
- Check the Original / Minified / Saved size cards for byte counts and percent saved.
- Copy (toast "Copied to clipboard!") or Download → minified.js (toast "Downloaded!").
- Re-minify after edits — the transform is click-triggered, not live.
- "Clear" resets both panes.
Tips
- URL https://… is protected from // comment stripping by the (?<!:) lookbehind — protocol strings survive.
- Keyword-space guards prevent returnx-style merges after operator squeezing.
- Not AST-based (the file comment says so) — verify output before production use.
- Strings containing // or /* are not specially protected — test carefully before shipping minified output.
- Since minify runs on click, you can batch several edits and minify once when ready.
JavaScript Minifier FAQ
Is it AST-based?
No — the lib header states "Basic minification only (no AST-based optimization)", so it's a regex pipeline, not a parser.
Does it keep URLs intact?
Yes — the (?<!:) lookbehind prevents stripping the // in :// so URLs like https://example.com survive.
What's the saved metric?
Blob byte sizes compared in compareSizes, shown as Original / Minified / Saved cards.
When does minify run?
On the "Minify" button click, not on every keystroke — empty input toasts "Please enter JavaScript to minify".
Does it upload my code?
No — the transform and Blob download are entirely local in lib/jsMinifier.ts.
Credits
- In-repo minifyJs — Regex-based JS minification in lib/jsMinifier.ts (no terser/uglify dependency)