← All blog posts

JavaScript Minifier explained with worked examples

WhiteUI · A plain-English guide to the JavaScript Minifier

The JavaScript Minifier does one job: it takes JavaScript written for people to read and returns the same code with the reading aids taken out. Comments go, blank lines go, and the spaces around punctuation go, while the code itself keeps doing exactly what it did. Four worked examples below show what that means in bytes, so you can see how big the saving is for different kinds of code.

At a glance

Default sample: 61 bytes0 bytes46 bytes93 bytes93 → 61 bytes, 32 saved (34%)
The default sample minifies from 93 bytes to 61 bytes, saving 32 bytes, or 34 percent.

Example 1: the default sample, 93 bytes to 61

The tool opens with a short greet function, 93 bytes long, with two comments and five lines. Minified, it becomes function greet(name){return “Hello, ” + name;}greet(“World”); which is 61 bytes. The saving is 32 bytes, or 34 percent. The two comments are gone, the line breaks are gone, and the spaces around the braces and the equals sign are gone. The space inside the string “Hello, ” stays, and the spaces around the + stay, because the tool keeps both on purpose.

Example 2: a small add function, 59 bytes to 32

Take a function that adds two numbers, with one comment and four lines, 59 bytes in total. Minified, it becomes function add(a,b){return a + b;} which is 32 bytes, saving 27 bytes, or 46 percent. The comment is gone, the line breaks are gone, and the spaces after the comma and around the braces are gone. The spaces around the + stay, because + is one of the operators whose spaces are never removed. Short functions with a comment save a large share, because the comment is a big part of the file.

Example 3: template literals and an arrow function, 111 bytes to 77

Try code that uses newer JavaScript: a template literal and an arrow function, 111 bytes with a comment. Minified, it becomes const msg=`Hello, ${name}!`;const shout=(text)=>{return text.toUpperCase();}; which is 77 bytes, saving 34 bytes, or 31 percent. The template literal is copied whole, including the ${name} inside the backticks, and the arrow function minifies normally. The comment is gone, the spaces around the equals signs are gone, and the line breaks are gone.

Example 4: a price calculator, 252 bytes to 155

Now a longer script: a function that works out a total with tax, plus two lines that call it and print the answer, 252 bytes with two comments. Minified, it is 155 bytes, saving 97 bytes, or 38 percent. The variable names subtotal and tax are unchanged — this tool does not rename anything — and the string “Total: ” keeps its space and its colon. The saving comes from the two comments, the line breaks, and the spaces around the punctuation, not from shortening names.

Why the savings differ

The saving depends on how much of the file is reading aids rather than code. A file full of comments and blank lines can shrink by half: a square function of 92 bytes, with a comment on nearly every line, minifies to 43 bytes, saving 49 bytes, or 53 percent. A file that is already dense saves far less: a short arrow function of 42 bytes, with no comments at all, minifies to 34 bytes, saving only 8 bytes, or 19 percent. There is no fixed percentage, only the share of the file that was comments, line breaks and spare spaces.

The common mistake

People expect the same percentage every time, then worry when a file only saves 19 percent. That is normal: the tool can only remove what is there to remove. A dense file with no comments has almost nothing to take away, while a commented file has plenty. Judge the result by the bytes saved, not by the percentage, and remember that even 8 bytes saved on a file that loads on every page is 8 bytes saved on every visit.

Frequently asked questions

How much does the JavaScript Minifier save on the sample?

The sample goes from 93 bytes to 61 bytes, saving 32 bytes, or 34 percent.

How much does a small function save?

An add function goes from 59 bytes to 32 bytes, saving 27 bytes, or 46 percent.

Does it work with template literals and arrow functions?

Yes. Template literals are copied whole, including ${name} inside backticks, and arrow functions minify normally.

Why do some files save more than others?

Because the saving comes from comments, line breaks and spare spaces. A commented file can save 53 percent; a dense file with no comments may save only 19 percent.

More guides on the JavaScript Minifier

Try it yourself

Try the JavaScript Minifier — free, in your browser.

Open the JavaScript Minifier