JavaScript Minifier
: Minify JavaScript with Terser: compress, mangle names and drop comments in your browserOptions
JavaScript Minifier
Minify JavaScript with Terser 5, the minifier behind many build pipelines. Paste a script or open a .js file and get back a smaller version with whitespace and comments removed, expressions simplified and local names shortened, with the before and after sizes (raw and gzip) shown next to it.
How to minify JavaScript
- Paste code into
JavaScript, or pressOpen fileto load a.js,.mjsor.cjsfile.Exampleloads a smallexport classand turns onES modulefor you. - Set the options you need. A
.mjsfile switchesES moduleon and a.cjsfile switches it off. If the code usesimport,exportor top-levelawait, turnES moduleon. - Input up to 256 KB is minified automatically about 300 ms after you stop typing. Larger input waits until you press
Minify, andCancelstops a run that is in progress. - Read the size line under
Minified: bytes before and after with the percentage change, plus the gzip size of each. If the code does not parse, the error names the line, andGo to lineputs the cursor there. - Press
Copy, orDownloadto save the result asname.min.js(a pasted script is saved asscript.min.js). WithSource mapon, theDownloadmenu also offers the map asname.min.js.map.
Example
A small classic script with the default options. The license banner is kept, the comment is dropped, the local variables get one-letter names, the loop is merged into one for statement and cartTotal keeps its name because top-level names of a classic script are not renamed.
/*! cart-total 1.0 | MIT */
// Sum the prices in a cart and apply a discount.
function cartTotal(items, discountPercent) {
var subtotal = 0;
for (var i = 0; i < items.length; i++) {
subtotal += items[i].price * items[i].quantity;
}
var discount = subtotal * (discountPercent / 100);
return Math.round((subtotal - discount) * 100) / 100;
}
/*! cart-total 1.0 | MIT */
function cartTotal(r,t){for(var a=0,n=0;n<r.length;n++)a+=r[n].price*r[n].quantity;var o=a*(t/100);return Math.round(100*(a-o))/100}Details
- Terser runs in a Web Worker in your browser, and your code is not uploaded or saved anywhere. It is loaded only when you first minify. If the worker cannot start, the same code runs on the main page instead.
- The example above went from 353 bytes to 160 bytes (about 54.7% smaller), and from 229 to 153 bytes with gzip. Small files gain less from gzip afterwards than large ones, because there is little repetition left to squeeze.
compressruns Terser's compressor with its default passes: dead code removal, constant folding, merging variable declarations, shorter equivalent expressions. It uses nounsafeoptions. Turn it off to get only whitespace and name changes.mangle namesshortens local variables, parameters and inner function names. In a classic script, top-level names are never renamed or dropped, since other scripts on the page may call them. WithES moduleon, top-level names are renamed too, because nothing outside the module can see them (exports keep their names).ES moduleparses the input as a module: strict mode,import/exportand top-levelawait. Leave it off for ordinary scripts. If the code fails in one mode but would parse in the other, the error message says which to switch.commentsset tokeep licenseleaves/*! … */comments and ones containing@license,@preserveor@copyright(Terser'ssomefilter, which also keeps@cc_on).remove alldrops every comment.output syntax(ES5,ES2015,ES2020) is the newest syntax Terser may introduce when it rewrites your code, for example arrow functions or optional chaining in place of longer forms. It does not convert your code: ES2020 input stays ES2020 even withES5selected, and the default isES5.keep function & class namesstops Terser from renaming functions and classes, sofn.nameandconstructor.namestill read as in the source. It makes the output a little larger.source mapproduces a map that embeds your original source, namedname.min.js.map, and adds asourceMappingURLcomment to the output. AsourceMappingURLcomment already in your input is removed with a warning, because that map no longer matches the new code.
Limits
- Large files up to 50 MB of UTF-8 work: over 256 KB they wait for
Minify, then show how long they have been running, withCancel(about 30 seconds for 20 MB). 50 MB is a memory limit, since Terser needs about 150 bytes of memory per byte of code and a tab that runs out closes; on a phone the limit can come sooner. Opening a file over 50 MB, or a file that is not valid UTF-8, shows an error. The file picker accepts.js,.mjsand.cjs. - This is a minifier, not a bundler: it does not follow
importstatements, join files or remove unused exports from other modules. Minify each file you paste. - TypeScript and JSX are not supported and fail as syntax errors. Compile them to plain JavaScript first, or tidy them with the Code Formatter.
- It does not transpile. Newer syntax in the input stays in the output; choosing
ES5only stops Terser from introducing newer syntax of its own. - There is no property renaming: object keys and
obj.propertynames are left alone, which is why the result is safe for code that reads properties by name. - Minifying is not obfuscation or protection. Shorter names and no whitespace make code harder to skim, but anyone can run a formatter over it, and strings, logic and any secrets inside are still readable.
FAQ
How do I minify JavaScript online without uploading it?
Paste the code or open the file here and read the result from output. The work is done by Terser in a worker inside your browser tab, so the code never leaves your device.
Does minifying JavaScript break code?
Not with the defaults, which use only Terser's safe transformations, leave top-level names of classic scripts and all property names alone, and do not change what the code does. Problems come from code that depends on function names (use keep function & class names), on eval or code built from strings, or from selecting the wrong ES module setting. Test the minified output like any other build.
What is the difference between minify and gzip?
Minifying rewrites the source into shorter source, which stays valid JavaScript. gzip compresses the bytes for transfer and the browser unpacks them. They stack: the stats line shows both, such as 229 bytes gzipped before and 153 bytes after for the example. Serve minified files with gzip or Brotli enabled for the smallest download.
Why did the file only shrink a little, or grow?
Code that is already minified has nothing left to remove, and the tool warns when the output is not smaller than the input. Tiny files can also gain nothing from renaming. Check that compress and mangle names are on.
Is minified JavaScript the same as obfuscated?
No. Minification aims at size and keeps the logic as it was; it is not a security measure. Pass the output through a formatter and most of the structure comes back, except the original local names and comments.
Runs entirely in your browser. Nothing is uploaded.