Cut the size of a JavaScript file
This JS minifier removes comments, indentation, line breaks and the spaces that only serve readability. The resulting script does exactly the same thing, with fewer bytes to transfer.
It suits a hand-written file, a snippet to embed in a page or a small project with no build step. Processing relies on the JShrink library, run on our server. A stylesheet or a page can be trimmed the same way with the CSS minifier or the HTML minifier.
How to use it
- Paste your JavaScript into the input box.
- Click Minify.
- Pick up the compact script in the result box.
- Click Copy, then save it to a separate file such as
app.min.js.
What the tool changes, and what it leaves alone
Take a six-line, 132-character function:
// Shows a welcome messagefunction greet(name) {var message = 'Hello ' + name;console.log(message);return message;}
The result is 84 characters on one line, 36% less:
function greet(name){var message='Hello '+name;console.log(message);return message;}
The comment and the spaces around operators are gone. The space inside the string 'Hello ' is kept, however: the content of strings is left untouched. The names greet, name and message stay as they are: the tool does not rename variables and does not remove unused code.
Two practical details. If your code is written without semicolons, the line breaks it needs are kept so the script stays valid. And every comment is removed, including licence notices starting with /*!: add them back by hand if the licence requires it. For a data file rather than a script, the JSON formatter and validator can minify it too.
Frequently asked questions
Is minified code unreadable to other people?
No. Minification is not obfuscation. Function and variable names are intact, and a code formatter restores the layout in a second. Do not leave passwords or secret keys in it.
Why does the tool show "error"?
Most often the code contains a string or a comment that is never closed, so the tool cannot split it correctly. Fix the source script and try again. An empty box gives the same message.
How much will I save on a real file?
It depends on how much commenting and indentation there is. A well-documented script can lose a good share of its weight, an already compact one almost nothing. Download time and parsing time in the browser drop accordingly.
Should I keep the original version?
Yes, always. Edit the source file, minify again, then test the page: a minified script is hard to debug.