How to Minify JavaScript with Terser

Last updated: · 3 min read

Every JavaScript file loaded by a web page affects how long the page takes to download and parse. The JavaScript Minifier uses the Terser engine to compress code: it removes whitespace and line breaks, rewrites expressions into shorter forms where possible and, if you choose, shortens local parameter names. Syntax errors are reported with their line and column. Processing takes place in your browser, so your code is never sent to a server.

Formula

1. Parse the code according to the JavaScript syntax; if there is an error, stop and report the line and column.
2. Remove whitespace, line breaks and comments from the output.
3. The compressor writes expressions in shorter equivalent forms that keep the same meaning, and drops dead code and unused values.
4. If 'Shorten' is selected, local variable and parameter names are replaced with shorter names (mangling).
5. Write the result as a single line.

This is not a mathematical formula but a parsing, transforming and printing algorithm. The syntax follows the ECMAScript specification (ECMA-262), and the minification is carried out by the Terser engine. Names that are shared with other code or accessed dynamically must be kept.

What the inputs mean

JavaScript
The JavaScript code to minify. It can contain functions, variables and comments. If the syntax is invalid, nothing is produced and the error line is shown.
Variable names
'Shorten' shortens local variable and parameter names; 'Keep' leaves the names unchanged, which is the safer choice for code that is accessed dynamically.

Worked example: Minifying a simple addition function

  1. Input: a function add(first, second) written over several lines, with return first + second; 'Variable names' is set to 'Shorten'.
  2. Line breaks, indentation and unnecessary spaces are removed.
  3. The parameters first and second are shortened to n and d, while the function name add stays the same.
  4. The body is compressed to return n+d and the whole code is written on one line.

Result: The output is function add(n,d){return n+d}; the behaviour is the same and the text is much shorter.

⚡ JavaScript Minifier

Free, no sign-up; your data stays in your browser.

Open the calculator →

How to read the result

Shortening local variable names is safe for local variables, but names that other code calls, or that are accessed dynamically through eval or object keys, can break. Use the 'Keep' option for that kind of code, and run the application's tests before you publish minified code. This is general information.

Common mistakes

  • Expecting the function name to be shortened too; in this example add stays, and only the parameter names are shortened.
  • Trying to minify code with a syntax error; an expression such as 'let x = ;' is reported with its line and column instead.
  • Shortening variable names in code that uses dynamic access, such as object keys or eval; the result can break in unexpected ways.
  • Keeping only the minified code as the source; debugging becomes very hard, so always keep the original file.
  • Assuming minification guarantees the same behaviour; test the application before release.

Frequently asked questions

Is my code sent to a server?

No. Terser runs in your browser and your code is not transmitted to any server.

Is it safe to shorten variable names?

For local variables it is usually safe. If names are accessed from outside or dynamically, choose 'Keep'.

Are function names changed too?

In this example the name add stayed the same and only the parameter names were shortened. Whether other names can be shortened depends on how your code is structured.

How much does minification save?

It depends on the code. It usually reduces the file size noticeably, and the gain grows when server compression is also used.

The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.

Sources

Choose your cookie preference to continue

Records the site needs to work (theme, language, favourites) stay in your browser.

This site is free and funded by ads.

With your consent, Google Analytics cookies measure visits and Google AdSense cookies are used to show ads; the data is processed on Google servers abroad.

If you reject, the site works just the same.

Cookie Policy · KVKK notice