← All blog posts

Common mistakes people make with the JavaScript Minifier

WhiteUI · A plain-English guide to the JavaScript Minifier

Most bad results from the JavaScript Minifier are not faults in the tool. They are small slips: code the tool was never meant to handle, an expectation it was never meant to meet, or an original file thrown away too soon. The tool itself uses exact rules, so when a result looks wrong, the input or the expectation is usually the cause, and each of the five mistakes below has a quick fix.

At a glance

Strings stay, regex literals change76 bytes minified9 bytes saved85 bytes → 76 bytes, 9 saved
A short example with a URL, a regex literal and a spaced string goes from 85 bytes to 76 bytes, saving 9 bytes.

Mistake 1: minifying a regex literal with repeated spaces

This is the one case where the tool changes what code means. A regular expression written directly, such as /a b/ with two spaces between the letters, is treated as ordinary code, and the two spaces are collapsed to one, giving /a b/. That matches something different. Strings in quotes are safe: “keep my spaces” comes out unchanged, and a URL like “https://example.com/a b” keeps its double space. But a bare regex literal is not a string, so check your code for one before you minify, and rewrite any regex that relies on repeated spaces.

Mistake 2: expecting renamed variables

People paste a script, see that subtotal is still subtotal, and think the tool did nothing. This tool does not rename variables or shorten names: the price script minifies from 252 bytes to 155 bytes, saving 97 bytes, and subtotal and tax are still there in full. The saving comes from comments, line breaks and spare spaces only. If you need shorter names, rename them yourself before you minify; bigger minifiers do that job, and this one does not claim to.

Mistake 3: pasting something that is not JavaScript

The box accepts any text, and it will happily minify HTML, CSS or a paragraph of English as if it were JavaScript, because it only looks for comments, quotes and punctuation. Paste a page of HTML and the result will be a smaller page of HTML, not working code. If the output looks strange, check what you pasted: it should be JavaScript only, with no tags, no style rules and no prose around it.

Mistake 4: throwing away the original

Minified code has no comments and no line breaks, so it is a poor thing to edit. People still delete the original and keep only the minified file, then discover they cannot remember what a line does. Keep the readable original in your project, and treat the minified file as a copy the tool makes in seconds whenever you need it. Re-minifying from the original is always safer than editing the packed copy.

Mistake 5: expecting a big saving on dense code

A short arrow function with no comments, 42 bytes long, minifies to 34 bytes, saving only 8 bytes, or 19 percent. People see that small number and think the tool failed. It did not: there was almost nothing to remove. The tool can only take away comments, line breaks and spare spaces, so a dense file saves little and a commented file saves a lot. Judge the result by the bytes saved, not by the percentage.

Worked check

Check any answer against the anchor: the default sample is 93 bytes and minifies to 61 bytes, saving 32 bytes, or 34 percent. If your sample shows something very different, you may have pasted extra text, or changed the sample before minifying. The anchor is the quick check that takes five seconds: 93 in, 61 out, 32 saved.

Frequently asked questions

What is the most common JavaScript Minifier mistake?

Minifying a regex literal with repeated spaces. /a b/ becomes /a b/, which matches something different, while strings in quotes stay unchanged.

Why are my variable names unchanged?

Because this tool does not rename variables. The saving comes from comments, line breaks and spare spaces only.

How much should a dense file save?

Not much. A 42-byte arrow function with no comments saves only 8 bytes, or 19 percent, because there is almost nothing to remove.

How do I check my result quickly?

Use the anchor: the default sample is 93 bytes and minifies to 61 bytes, saving 32 bytes, or 34 percent.

More guides on the JavaScript Minifier

Try it yourself

Try the JavaScript Minifier — free, in your browser.

Open the JavaScript Minifier