JavaScript minifier
Smaller, and provably the same code.
Examples temporarily replace the input and settings. You can restore your previous work.
Paste or type above to see the result here.
Your files stay on your device
This tool runs entirely in your browser. What you paste is processed on your device and is never sent to a server, so it is safe to use with tokens, keys and unpublished text.
Example
Minifying a bakery’s cart script: 714 characters down to 503, licence line kept
Maple Street Bakery’s checkout loads cart-total.js: 22 lines that add up the cart in cents, take off a coupon, add Austin’s 8.25% sales tax and print a summary line. It opens with a /*! MIT licence comment and has three ordinary comments. Setting: Minify.
- The figures read 714 before, 503 after, 30% saved. The 22 lines become 5: the licence comment keeps its own line, and a break after a closing } stays, as only breaks proven safe are removed.
- The /*! cart-total.js v1.4.0 | Maple Street Bakery | MIT License */ line stays at the top; the three other comments, // and /* */, are gone.
- The summary template literal keeps its double spaces around + and =: nothing inside quotes or backticks is touched.
- Both versions give the same answer for six muffins at $4.25, a $18.99 cake and a $3.00 coupon: subtotal 4149, tax 342, total 4491 cents, printed as $41.49 + $3.42 = $44.91.
- Switched to Just count the pieces, the code stays as pasted and the figure reads 12 pieces: runs of code plus each string and template, comments left out.
| Part | Before | After |
|---|---|---|
| Licence line | /*! cart-total.js v1.4.0 | Maple Street Bakery | MIT License */ | kept, unchanged |
| Loop body | cents += item.priceCents * item.qty; | cents+=item.priceCents*item.qty; |
| Tax | const tax = Math.round(cents * 0.0825); | const tax=Math.round(cents*0.0825); |
| Money | return (cents / 100).toLocaleString('en-US', { style: 'currency', currency: 'USD' }); | return(cents/100).toLocaleString('en-US',{style:'currency',currency:'USD'}); |
| Whole file | 22 lines, 714 characters | 5 lines, 503 characters (30% saved) |
cartTotal, couponCents and priceCents keep their names, so the page that calls cartTotal() needs no change: upload the 503-character file in place of the old one. A build tool that renames variables can squeeze out a little more.
How it works
- Paste the JavaScript.
- Comments and indentation go; names stay exactly as they were.
- Check the saving in the figures above the output.
- Copy or download the result.
Frequently asked questions
- Why does it not shorten my variable names?
- Because renaming needs a real parser and a scope analysis, and getting that subtly wrong ships a file that runs until it doesn’t. A tool that promises less and is always right is the better trade for something you are about to put in front of users. For the last few percent, use esbuild or terser in your build.
- Can it tell a regular expression from a division?
- Yes — that is the one genuinely hard part of reading JavaScript without parsing it. A slash after a value or a closing bracket is division; after an operator, an opening bracket, a comma, a word such as return or typeof, or at the start, it opens a regular expression. So x = /ab+c/g and return /a b/.test(s) survive whole, spaces included, and total / 2 is still a division.
- Will it break automatic semicolon insertion?
- No. A newline can stand in for a semicolon, so lines are only joined when the break cannot be one: the line before ends in an operator, a comma or an opening bracket, or this one starts with a closing bracket, a dot or a comma. A line ending in ++ or -- or in a regular expression keeps its break, so code written without semicolons comes out working too.
- Is my licence header removed?
- No. A block beginning /*! and any comment containing @license or @preserve is kept exactly as written, line breaks included, on a line of its own. Every other // and /* */ comment goes, JSDoc /** blocks too.
- Is the saving counted in bytes?
- In characters: before and after are the length of the text as JavaScript counts it. For code in plain English letters that equals the bytes; an accented letter such as é takes two bytes in UTF-8, so a file with accented comments saves a little more in bytes than the percentage shows.
- Is anything sent to a server?
- No. It runs in your browser, so what you paste stays on your computer.