What is a code minifier?
A code minifier takes formatted code and shrinks it — comments out, whitespace down — so the file is smaller and loads faster, while the code does exactly what it did before. Paste in a small function with a comment and a few blank lines, and the minifier gives you the same function on one line, 30 characters instead of 59, saving 49.2%. The behaviour is identical; only the size changed.
The need comes up whenever code has to travel. A web page loads its JavaScript and CSS over a network, and every byte costs time — on a slow connection, a file half the size loads in half the time. Developers write code to be read, with comments and indentation, and then minify it to be shipped, with neither. The minifier is the tool that turns the first into the second.
This minifier handles three languages: JavaScript, CSS, and HTML. Pick the language, paste the code, and the minified version appears at once, with the percentage saved and the before-and-after sizes above it. JavaScript is the default, because a script file is the most common thing anyone minifies.
It runs entirely in your browser. Nothing is uploaded, nothing is stored, and there is no account to create. Your code never leaves your device, which matters when the code belongs to a project you are not ready to share.
How light minification works: comments out, whitespace down
Light minification does two things and nothing else. First, it strips the comments: line comments starting with // and block comments between /* and */ in JavaScript and CSS, and comments between <!-- and --> in HTML. Comments are written for humans, and a browser does not read them — removing them saves space and changes nothing about how the code runs. The default example loses its '// add two numbers' comment, which is eleven characters of explanation the browser never needed.
Second, it collapses the whitespace. Runs of spaces, tabs, and newlines become a single space — or no space at all, where no space is needed. A space between two words must stay, because 'return a' without the space would be 'returna', which is a different word. But a space between a brace and a word can go, because '{return' reads exactly the same as '{ return' to a machine. The minifier keeps a space only where two word characters meet, and drops it everywhere else.
Strings are never touched. A space inside a quoted string is part of the text, not formatting, and removing it would change what the program prints. The minifier tracks single quotes, double quotes, and backticks, and everything inside them is copied exactly — including '//' that looks like a comment but is really part of a message. The test case in the worked example proves it: a string containing '// not a comment' keeps its slashes and its spaces.
That is the whole of light minification, and the word 'light' is doing real work here. A full minifier also renames variables, drops unused code, and rewrites expressions — and those changes can break code if they are done wrong. This minifier does none of that. It removes only what is provably useless: comments and whitespace. The code that comes out is smaller, and it is the same code.
JavaScript, CSS, and HTML: three different jobs
JavaScript minification is comment-stripping plus whitespace collapsing. Both kinds of comment go — the // line comment and the /* */ block comment — and the whitespace collapses to a single space only where two word characters meet. The default function goes from 59 characters to 30, saving 49.2%, and the result is function add(a,b){return a+b;} — one line, no comment, no wasted space, same behaviour.
CSS minification goes one step further. After the comments and the whitespace, it also removes the spaces around the punctuation that structures a rule: the braces, the colon, the semicolon, and the comma. A rule written as body { color: red; margin: 0; } becomes body{color:red;margin:0} — 24 characters instead of 49, saving 51.0%. It also drops the last semicolon before a closing brace, because a semicolon with nothing after it is doing no work.
HTML minification is different again, because HTML is not code with statements — it is text with tags. The minifier strips the comments, then removes the whitespace between a closing tag and the next opening tag, and collapses any whitespace left inside the text to a single space. The example <!-- hi --><div><p>Hello</p></div> becomes <div><p>Hello</p></div> — 23 characters instead of 39, saving 41.0%. The words 'Hello' keep their space, because it is inside the text, not between tags.
The three modes share one rule: never touch a string, and never rename anything. A variable called 'add' stays called 'add', a class called 'body' stays called 'body', and a word in the text stays exactly as it was written. That is what makes light minification safe to run on any code, without reading it first.
A worked example: three files, three savings
Start with the default: JavaScript, a function with a comment. The input is 59 characters: function add(a, b) {, a line comment, return a + b;, and a closing brace. The output is 30 characters: function add(a,b){return a+b;}. The comment is gone, the spaces around the comma and the braces are gone, and the saving is 49.2% — nearly half the file, from the two simplest operations there are.
Switch to CSS and paste /* comment */ body { color: red; margin: 0; }. The input is 49 characters. The output is body{color:red;margin:0} — 24 characters, saving 51.0%. The comment is gone, the spaces around the braces, the colon, and the semicolons are gone, and the trailing semicolon before the closing brace is gone too. The rule still sets the same two properties on the same element.
Switch to HTML and paste <!-- hi --><div><p>Hello</p></div> with its line breaks. The input is 39 characters. The output is <div><p>Hello</p></div> — 23 characters, saving 41.0%. The comment is gone, the whitespace between the tags is gone, and the word 'Hello' is untouched, because it is text, not formatting.
One more check worth doing: a string with a fake comment in it. Paste var s = "a // not a comment"; var t = 5; // real, then var u = 6;. The output is var s="a // not a comment";var t=5;var u=6;. The '// not a comment' inside the quotes survived, with its spaces, while the real comment after the semicolon vanished. That is the string rule working: the minifier knows the difference between code and text, and it only removes what is code.
Who minifies code, and why
Web developers minify code before they ship it. A website loads its JavaScript and CSS on every visit, and smaller files mean faster pages — especially on a slow mobile connection, where a file half the size can be the difference between a page that feels instant and one that feels broken. Minification is one of the cheapest speed wins there is: the code is the same, the file is smaller, and the user waits less.
Performance engineers minify as part of a build pipeline. The code is written readably, tested readably, and then minified automatically when it is deployed — so the file on the server is always the small one, and the file in the editor is always the readable one. A minifier in the pipeline means nobody has to choose between readable code and fast pages.
Learners minify to understand what matters. A beginner who minifies a function and reads the result can see what the machine actually needs: the words, the braces, the semicolons — and nothing else. The comments and the indentation were for the human, and the minified version shows what is left when the human is taken out of the picture.
Then there are the everyday uses: shrinking a snippet before pasting it into a page, reducing a config file before sending it, or checking how much smaller a file could be before deciding whether minification is worth setting up. A text formatter tidies plain prose the same way this page shrinks code — one for words, one for programs.
Code minifier vs code formatter
This minifier and the code formatter are opposites, and they are meant to be used together. The formatter takes small, dense code and spreads it out so a human can read it. The minifier takes spread-out code and packs it down so a machine can load it fast. One is for reading, one is for shipping — and the workflow is to format while you work, and minify when you deploy.
The numbers show the opposite directions clearly. The formatter turns the default JSON object from 38 characters into 51 — bigger, because it adds line breaks and indentation. The minifier turns the default function from 59 characters into 30 — smaller, because it removes the comment and the whitespace. Same idea, opposite sign: formatting adds the space a human needs, minification removes the space a machine does not.
Use the formatter when the code is hard to read, and the minifier when the code is ready to ship. Never ship formatted code — it is bigger for no benefit — and never edit minified code — it is unreadable for no benefit. Keep the readable copy, minify a copy of it, and ship the minified copy. If you ever need to edit it again, go back to the readable copy, not the minified one.
One more neighbour worth knowing: the markdown editor formats prose, not code — it turns markdown marks into HTML. Code and prose are different materials, and each has its own tools. Do not paste code into a prose tool and expect it to understand the braces; do not paste prose into a minifier and expect it to understand the sentences.
Common minification mistakes
The most common mistake is minifying the only copy. Minified code is unreadable by design — one line, no comments, no indentation — and editing it is a misery. Keep the readable original, minify a copy, and ship the copy. If the original is lost, the minified version is all you have, and recovering the readable code from it is a job nobody wants.
Second: expecting minification to fix slow code. Minification makes a file smaller, which makes it load faster — but it does not make the code run faster. A slow function minified is still a slow function, only smaller. If the page is slow after it loads, the problem is the logic, not the file size, and no minifier will fix it.
Third: minifying HTML with meaningful whitespace. In HTML, a space between two words is text, and the minifier keeps it — but a space between two inline elements can also be text, and collapsing it can join two words that should stay apart. The minifier collapses whitespace inside text to a single space, which is safe for most pages, but if your page relies on exact spacing between elements, check the result before you ship it.
Fourth: forgetting that minified code still needs gzip. Minification and compression are different steps, and they stack: a file that is minified and then gzipped is smaller than one that is only minified. Serve the minified file with gzip, and the saving is larger than either step alone. And fifth: minifying by hand. A hand-minified file is a file with a mistake in it — a missing space that joins two words, a comment removed that held a licence notice. Let the tool do it, and keep the original.
How to use this code minifier
Using it takes a few seconds. Pick the language — JavaScript is the default — paste your code into the box, and the minified version appears instantly in the box below, with the percentage saved and the before-and-after sizes above it. To use the minified code somewhere else, copy it from the output box with the Copy button.
To check a single case, paste a function with a comment and watch the comment vanish and the lines join. To check that strings are safe, paste a string containing '//' and watch it survive untouched. A JSON formatter can check the JSON case if you need it.
Everything runs in your browser, so nothing you paste is uploaded or stored. One practical tip: minify a copy, never the original. Keep the readable file, ship the minified file, and if you ever need to change the code, change the readable file and minify it again. The minified file is a product, not a source.
And if the output ever looks wrong — a word joined to another word, a string changed — stop and check the input. Light minification only removes comments and whitespace, and it never touches a string. If something changed that should not have, the input probably had a string the minifier could not see, such as a quote opened in one place and closed in another.