Skip to main content

Minification

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/minification

In short

Minification is the process of shrinking code files by removing whitespace, comments and long names without changing behavior, so web pages download faster.

What is minification?

Minification turns source code into the smallest version that still does exactly the same thing. For JavaScript, a minifier removes spaces, line breaks, and comments, shortens local variable and parameter names to a letter or two, and rewrites some expressions into shorter equivalents. CSS and HTML can be minified in the same way. The output is hard for people to read, but browsers don't mind.

Modern minifiers such as Terser, esbuild, SWC, and Lightning CSS first parse the code into a syntax tree, so they know which names are safe to rename and which code can never run and can be dropped. Minification usually happens automatically in a production build, and a source map is generated alongside the output so error messages and debugger breakpoints still point to your original code. Savings of 30 to 60 percent before compression are common.

Minification is like turning a nicely formatted recipe into a text message: every step is still there, just without the spacing, headings, and friendly explanations. Smaller files mean less data to download and less text for the browser to parse, which improves loading metrics such as Largest Contentful Paint, especially on slow mobile connections.

Minification is often confused with compression and obfuscation. Compression, such as gzip or Brotli, is applied by the server when sending a file and undone by the browser, while minification permanently changes the file; the two stack, and using both gives the smallest transfer size. Obfuscation deliberately makes code hard to understand to discourage copying and often makes files larger. Minification is not a security measure either: anyone can reformat minified code, and secrets in front-end code stay visible.

Key takeaways

  • Minification removes whitespace, comments, and long names without changing behavior.
  • It applies to JavaScript, CSS, and HTML and usually runs in production builds.
  • Source maps connect minified code back to the original for debugging.
  • Server compression such as gzip or Brotli works on top of minification.
  • Minified code is not secret; it can be reformatted and read.

Example

The same function before and after minificationjavascript
// Before minification (readable source)
function calculateTotal(items, taxRate) {
  // Sum item prices, then add tax
  const subtotal = items.reduce((sum, item) => sum + item.price, 0);
  return subtotal * (1 + taxRate);
}

// After minification (same behavior, far fewer bytes)
function calculateTotal(t,e){return t.reduce((t,e)=>t+e.price,0)*(1+e)}

// Typical commands:
// npx terser src/app.js --compress --mangle -o dist/app.min.js
// npx esbuild src/app.js --minify --outfile=dist/app.min.js

Readers ask

What is the difference between minification and compression?

Minification rewrites the file itself into a smaller but equivalent version at build time. Compression, such as gzip or Brotli, encodes the file for transfer and the browser decodes it on arrival; combining both gives the best result.

Can minification break my code?

A correct minifier only changes the form of the code, not its behavior. Rare problems come from code that depends on function or class names at runtime, such as constructor.name, which minifiers may rename unless configured to keep them.

What is a .min.js file?

It is a naming convention for a minified JavaScript file, such as app.min.js, often published next to the readable app.js. Build tools that hash file names for caching usually skip the .min suffix.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings