JavaScript Minifier

Minify JavaScript with Terser, the minifier behind most modern build tools. Shorten variable names, drop dead code, and see the size saving raw and gzipped.

Updated
Runs in your browser. Your data is not uploaded.
Terser loads on first use (about 140 KB).

How to use the JavaScript Minifier

  1. Paste JavaScript into Code input, or upload or drop a .js file.
  2. Choose the options: Shorten names, Remove console calls, ES module, and Keep license comments.
  3. Press Minify. The first run loads Terser (about 140 KB), then copy or download the result and check the size summary.

How it works

Terser parses the code into a syntax tree, simplifies it, and prints it back with as few characters as possible.

  • Compress folds constants, removes unreachable code and unused functions, and joins consecutive statements.
  • Shorten names (mangling) renames local variables and parameters to one or two letters. Global names and object properties are never renamed, so other scripts can still call your code.
  • ES module parses import and export and treats top-level names as local, which allows more renaming.
  • Remove console calls deletes console.log and friends.

Examples

Input:

// Adds tax to a price
function priceWithTax(price, rate) {
  const tax = price * rate;
  if (false) {
    console.log('never runs');
  }
  return price + tax;
}
window.priceWithTax = priceWithTax;

Output with all options on except ES module:

function priceWithTax(i,r){return i+i*r}window.priceWithTax=priceWithTax;

The dead if (false) block and the comment are gone, tax is inlined, and the parameters are shortened. The global function name is kept.

Limitations

  • TypeScript and JSX must be compiled to JavaScript first.
  • No source map is produced, so keep the original file for debugging.
  • Code that reads function names at run time, or calls eval, can break when names are shortened. Untick Shorten names in that case.
  • Files are limited to 5 MB. Very large files can take several seconds.

Frequently asked questions

What is the difference between minifying and obfuscating?

Minifying makes code smaller and faster to download; the result is hard to read as a side effect. Obfuscation deliberately makes code hard to understand and often makes it bigger and slower.

Can minification break my code?

Rarely. The usual causes are code that depends on function or variable names at run time, or missing semicolons combined with unusual line breaks. Test the minified file before you ship it.

Why is the gzipped saving smaller than the raw saving?

Gzip already compresses repeated whitespace and long names well. The gzipped figures are what visitors actually download from a server with compression turned on.

Is my code uploaded?

No. Terser runs in your browser, and your code never leaves your device.

Often used together with the JavaScript Minifier.