Skip to main content

Code minification

What minification is

Minification is the process of removing everything unnecessary from source code (JS, CSS, HTML) that does not affect the program's execution, but reduces file size.

After minification, the code stays correct but becomes as compact as possible.

What exactly gets removed

What is removedExample
Whitespace and tabsfunction test () { ... } -> function test(){...}
Line breaks\n -> removed
Comments/* description */ -> removed
Long variable nameslet counter = 0; -> let a=0;
Unused code (dead code)Removed via tree-shaking
Redundant semicolons, bracketsOptimized

Example

Before minification:

javascript
function sum(a, b) { // add two numbers return a + b; } console.log(sum(2, 3));

After minification:

javascript
function sum(n,o){return n+o}console.log(sum(2,3));

Before: ~90 bytes After: ~45 bytes Minus 50% of the size, and with gzip/brotli compression the savings are even bigger.

How this affects speed

When loading, the browser needs to:

  1. Download the JS/CSS/HTML files;
  2. Parse, execute, and render the page.

The smaller the file weight, the:

  • faster the network latency;
  • faster the parsing and execution;
  • lower the time to first render (FCP, LCP);
  • lower the load on the server and CDN.

Benefits of minification

BenefitWhat it gives
Faster loadingLess data -> a faster page
Fewer requests to the serverSaves traffic
Faster on mobileReduces battery and CPU usage
Improved SEOGoogle favors fast sites
Harder to read the codeSlightly raises the bar (but is not obfuscation)

Where it is used

Minification is applied to:

  • JavaScript (.js)
  • CSS (.css)
  • HTML (.html)
  • JSON and SVG (often automatically)
  • Sometimes even inline scripts and styles

How minification differs from obfuscation

ParameterMinificationObfuscation
GoalReduce sizeHide the code's logic
Affects performanceYes, speeds things upNo
Code readabilityBecomes less readableNearly impossible to understand
Example toolsTerser, UglifyJS, cssojavascript-obfuscator, obfuscator.io

Tools

LanguagePopular tools
JSTerser, UglifyJS, SWC, esbuild
CSScssnano, CleanCSS, csso
HTMLhtml-minifier, posthtml-minifier
BundlersWebpack, Vite, Rollup, Parcel - do this automatically with NODE_ENV=production

Example for Webpack:

javascript
// webpack.config.js optimization: { minimize: true, minimizer: [ new TerserPlugin(), new CssMinimizerPlugin(), ], }

In Next.js, React, Vite, and other modern bundlers

Minification is enabled by default in a production build:

javascript
npm run build

The bundler automatically:

  • minifies JS/CSS;
  • removes unused code (tree-shaking);
  • applies gzip/brotli compression on deploy (for example, via Vercel/CDN).

Example of the real-world benefit

File typeBefore minificationAfter minificationSavings
JS bundle1.2 MB340 KB-72%
CSS bundle280 KB85 KB-70%
HTML45 KB30 KB-33%

On a poor mobile connection, this saves 1-2 seconds of load time!

Summary

What it doesWhy
Removes unnecessary characters, whitespace, and commentsReduces file size
Speeds up page loadingImproves UX and SEO
Reduces load on the network and serverSaves resources
Applied automatically during the buildSimple, "free" optimization

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.