← All blog posts

How to get an accurate result from the JavaScript Minifier

WhiteUI · A plain-English guide to the JavaScript Minifier

An accurate result from the JavaScript Minifier comes from two habits: pasting clean JavaScript in, and checking the numbers and the code before you use the result. The tool already uses exact rules, so most accuracy problems come from pasting something that is not JavaScript, or from trusting the byte count without running the code.

At a glance

Template literal example: 31% smaller77 bytes minified34 bytes saved111 bytes → 77 bytes, 34 saved
A template literal and arrow function example goes from 111 bytes to 77 bytes, saving 34 bytes, or 31 percent.

Paste clean JavaScript only

The box accepts any text, so it will happily minify HTML, CSS or a paragraph of English as if it were JavaScript. If the result looks strange, check what you pasted: it should be JavaScript only, with no tags, no style rules and no prose around it. Paste the whole file or the whole function, not half of one: a function cut in half minifies to a broken half-function, and the byte count will look right while the code is wrong.

Check the three numbers

Read all three lines after every change: original, minified and saved. For the add function the tool should show Original: 59 characters · Minified: 32 characters and Saved: 27 characters. For the default sample it should show 93, 61 and 32. If any of the three looks wrong, something was pasted or edited after generating: the numbers always agree with each other, because the tool measures the text it actually built.

Check the strings survived

Open the minified output and look at every string in it. “Hello, ” should still have its trailing space, “Total: ” should still have its space after the colon, and a template literal should still have its ${name} inside the backticks. In the price script, which minifies from 252 bytes to 155 bytes, the string “Total: ” is still there in full. Strings are copied untouched, so a changed string means the text was edited after minifying, not a fault in the tool.

Watch out for regex literals

The one check that matters most is for a regular expression written directly in the code. A regex literal such as /a b/, with two spaces between the letters, is treated as ordinary code, and the two spaces collapse to one, giving /a b/, which matches something different. A short example shows the scale: a file with a URL, a regex literal and a spaced string goes from 85 bytes to 76 bytes, saving 9 bytes, and the only change you would not want is the regex. Search your code for / before you minify, and rewrite any regex that relies on repeated spaces.

Run the minified code once

Finally, run the result. Paste it into your browser console, or load the page and use what the code does. The square function should still return 36 for square(6), the add function should still return the sum, and the greet function should still return “Hello, World”. A byte count tells you the file got smaller; only running the code tells you it still works. That one-minute check is worth doing every time, not just the first time.

The common mistake

People trust the byte count without running the code, because a 34 percent saving feels like proof the job is done. It is not: a smaller file can still be a broken file, especially if it had a regex literal in it. The saved number measures size, not correctness. Minify, check the three numbers, check the strings, run the code once, and only then put it on a live page.

Frequently asked questions

How do I know my minified result is accurate?

Check the three numbers, check the strings survived, and run the code once. square(6) should still return 36.

What should the add function show?

Original: 59 characters, Minified: 32 characters, Saved: 27 characters.

What should I check in the strings?

That “Hello, ” still has its trailing space and a template literal still has its ${name} inside the backticks.

What breaks accuracy most often?

A regex literal with repeated spaces. /a b/ becomes /a b/, which matches something different.

More guides on the JavaScript Minifier

Try it yourself

Try the JavaScript Minifier — free, in your browser.

Open the JavaScript Minifier