DIY Minification

No minifier can compress bad code properly.

In this example, I just want to show how much a minifier does.

What you should do before you minify

And regarding jQuery... I don't use jQuery. jQuery is for old browsers; it was made for compatibility reasons. Check Can I use; almost everything works in every browser (also Internet Explorer 10 is standardized now). I think now it's just here to slow down your web application... If you like the $(), you should create your own simple function. And why bother to compress your code if your clients need to download the 100 KB jQuery script every time? How big is your uncompressed code? 5-6 KB...? Not to talk about the tons of plugins you add to to make it easier.

Original Code

When you write a function you have an idea, start to write stuff and sometimes you end up with something like the following code.The code works.Now most people stop thinking and add this to a minifier and publish it.

function myFunction(myNumber){
    var myArray = new Array(myNumber);
    var myObject = new Object();
    var myArray2 = new Array();
    for(var myCounter = 0; myCounter < myArray.length; myCounter++){
        myArray2.push(myCounter);
        var myString = myCounter.toString()
        myObject[myString] = (myCounter + 1).toString();
    }
    var myContainer = new Array();
    myContainer[0] = myArray2;
    myContainer[1] = myObject;
    return myContainer;
}

Here is the minified code (I added the new lines):

Minified using (http://javascript-minifier.com/)

function myFunction(r){
 for(var n=new Array(r),t=new Object,e=new Array,a=0;a<n.length;a++){
  e.push(a);
  var o=a.toString();
  t[o]=(a+1).toString()
 }
 var i=new Array;
 return i[0]=e,i[1]=t,i
}

But are all those variables, ifs, loops, and definitions necessary?

Most of the time, NO!

  1. Remove unnecessary if,loop,var
  2. Keep a copy of your original code
  3. Use the minifier

OPTIONAL (increases the performance & shorter code)

  1. use shorthand operators
  2. use bitwise operators (don't use Math)
  3. use a,b,c... for your temp vars
  4. use the old syntax (while,for... not forEach)
  5. use the function arguments as placeholder (in some cases)
  6. remove unneccessary "{}","()",";",spaces,newlines
  7. Use the minifier

Now if a minifier can compress the code your doing it wrong.

No minifier can compress properly a bad code.

DIY

function myFunction(a,b,c){
 for(b=[],c={};a--;)b[a]=a,c[a]=a+1+'';
 return[b,c]
}

It does exactly the same thing as the codes above.

Performance

http://jsperf.com/diyminify

You always need to think what you need:

Before you say "No one would write code like the one below" go and check the first 10 questions in here ...

Here are some common examples I see every ten minutes.

Want a reusable condition

if(condition=='true'){
 var isTrue=true;
}else{
 var isTrue=false;
}
//same as
var isTrue=!!condition

Alert yes only if it exists

if(condition==true){
 var isTrue=true;
}else{
 var isTrue=false;
}
if(isTrue){
 alert('yes');
}
// The same as
!condition||alert('yes')
// If the condition is not true alert yes

Alert yes or no

if(condition==true){
 var isTrue=true;
}else{
 var isTrue=false;
}
if(isTrue){
 alert('yes');
}else{
 alert('no');
}
// The same as
alert(condition?'yes':'no')
// If the condition is true alert yes else no

Convert a number to a string or vice versa:

var a=10;
var b=a.toString();
var c=parseFloat(b)
// The same as
var a=10,b,c;
b=a+'';
c=b*1

// Shorter
var a=10;
a+='';// String
a*=1;// Number

Round a number

var a=10.3899845
var b=Math.round(a);
// The same as
var b=(a+.5)|0; // Numbers up to 10 decimal digits (32bit)

Floor a number

var a=10.3899845
var b=Math.floor(a);
// The same as
var b=a|0;//numbers up to 10 decimal digits (32bit)

switch case

switch(n)
{
case 1:
  alert('1');
  break;
case 2:
  alert('2');
  break;
default:
  alert('3');
}

// The same as
var a=[1,2];
alert(a[n-1]||3);

// The same as
var a={'1':1,'2':2};
alert(a[n]||3);

// Shorter
alert([1,2][n-1]||3);
// Or
alert([1,2][--n]||3);

try catch

if(a&&a[b]&&a[b][c]&&a[b][c][d]&&a[b][c][d][e]){
 console.log(a[b][c][d][e]);
}

// This is probably the only time you should use try catch
var x;
try{x=a.b.c.d.e}catch(e){}
!x||conole.log(x);

More if

if(a==1||a==3||a==5||a==8||a==9){
 console.log('yes')
}else{
 console.log('no');
}

console.log([1,3,5,8,9].indexOf(a)!=-1?'yes':'no');

But indexOf is slow. Read this: How do I check if an array includes a value in JavaScript?

Numbers

1000000000000
// The same as
1e12

var oneDayInMS=1000*60*60*24;
// The same as
var oneDayInMS=864e5;

var a=10;
a=1+a;
a=a*2;
// The same as
a=++a*2;

Some nice articles/sites I found about bitwise/shorthand:

http://mudcu.be/journal/2011/11/bitwise-gems-and-other-optimizations/

http://www.140byt.es/

http://www.jquery4u.com/javascript/shorthand-javascript-techniques/

There are also many jsperf sites showing the performance of shorthand & bitwise if you search with your favorite search engine.

I could go one for hours.. but I think it's enough for now.

If you have some questions, just ask.

And remember:

No minifier can compress properly bad code.

Answer from cocco on Stack Overflow
🌐
Minify JS
minify-js.com
Minify JS Online. JavaScript Minification tool that works in browser. | Minify JS Online
Minification stands by reducing source code size by converting it to the optimized version removing unnecessary characters, comment blocks, shortening local variables and function names. In some cases, minification can save up to 80% of the javascript file size.
🌐
Toptal
toptal.com › developers › javascript-minifier
JavaScript Minifier & Compressor | Toptal®
Use our JavaScript Minifier & Compressor tool to reduce JavaScript code size and make your website load faster. Get started for free now.
Discussions

gzip - How to 'minify' JavaScript code, like jQuery is minified - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... jQuery has two versions for download. One is Production (19 KB, minified and gzipped), and the other is Development (120 KB, uncompressed code). More on stackoverflow.com
🌐 stackoverflow.com
What exactly is a minified JavaScript file?
Usually, minification goes further. Replacing the names of variables and internal methods, even rewriting certain patterns to be smaller. In most cases, the code won't be faster, only load slightly faster (usually, gzip will save less bytes). However loading time is only an issue when you don't cache. More on reddit.com
🌐 r/javascript
20
8
June 13, 2015
Best way to minify JS, CSS and HTML? Anyone use Gulp.js?
Cloudflare will do it More on reddit.com
🌐 r/TechSEO
4
3
March 30, 2017
Best / simplest way to minify javascript?
Afaik, you don't 'minify' javascript. You uglify it. https://atom.io/packages/uglify More on reddit.com
🌐 r/linux4noobs
10
2
August 13, 2018
People also ask

What is JavaScript Minification?
Minification, or minimization, of JavaScript source code is the process removing all characters that aren't required for proper execution. These unnecessary characters usually include formatting characters, like: whitespaces, linebreak characters, comments, and in some cases block delimeters and end-of-line characters. After minification is applied, JS code is supposed to keep its functionality.
🌐
minify-js.com
minify-js.com
Minify JS Online. JavaScript Minification tool that works in browser.
How does JavaScript Minification work?
Minification process is performed by a software or utility that analyzes and rewrites source code to reduce its size. Usually, minification process includes removal of whitespaces, shortening of variable names, and verbose functions replacement. Minification is performed on the server side and only when the source file is changed.
🌐
minify-js.com
minify-js.com
Minify JS Online. JavaScript Minification tool that works in browser.
Why is Minification used?
Minification allows to reduce JavaScript file size that has a positive impact on load times and bandwidth usage. As a result, site speed and accessibility is higher compared to sites that don't use minification. Other words, minification tangibly improves user experience.
🌐
minify-js.com
minify-js.com
Minify JS Online. JavaScript Minification tool that works in browser.
🌐
Minifier
minifier.org
Minify JS and CSS online, or include the minifier in your project for on-the-fly compression.
This JS and CSS minifier removes whitespace, strips comments, combines files, and optimizes/shortens a few common programming patterns.
🌐
Code Beautify
codebeautify.org › minify-js
Minify JS is JavaScript Minifier online
Online JavaScript Minify helps to Minify and Compress JavaScript data. It reduce the size of JavaScript and remove unwanted spaces.
Top answer
1 of 9
53

DIY Minification

No minifier can compress bad code properly.

In this example, I just want to show how much a minifier does.

What you should do before you minify

And regarding jQuery... I don't use jQuery. jQuery is for old browsers; it was made for compatibility reasons. Check Can I use; almost everything works in every browser (also Internet Explorer 10 is standardized now). I think now it's just here to slow down your web application... If you like the $(), you should create your own simple function. And why bother to compress your code if your clients need to download the 100 KB jQuery script every time? How big is your uncompressed code? 5-6 KB...? Not to talk about the tons of plugins you add to to make it easier.

Original Code

When you write a function you have an idea, start to write stuff and sometimes you end up with something like the following code.The code works.Now most people stop thinking and add this to a minifier and publish it.

function myFunction(myNumber){
    var myArray = new Array(myNumber);
    var myObject = new Object();
    var myArray2 = new Array();
    for(var myCounter = 0; myCounter < myArray.length; myCounter++){
        myArray2.push(myCounter);
        var myString = myCounter.toString()
        myObject[myString] = (myCounter + 1).toString();
    }
    var myContainer = new Array();
    myContainer[0] = myArray2;
    myContainer[1] = myObject;
    return myContainer;
}

Here is the minified code (I added the new lines):

Minified using (http://javascript-minifier.com/)

function myFunction(r){
 for(var n=new Array(r),t=new Object,e=new Array,a=0;a<n.length;a++){
  e.push(a);
  var o=a.toString();
  t[o]=(a+1).toString()
 }
 var i=new Array;
 return i[0]=e,i[1]=t,i
}

But are all those variables, ifs, loops, and definitions necessary?

Most of the time, NO!

  1. Remove unnecessary if,loop,var
  2. Keep a copy of your original code
  3. Use the minifier

OPTIONAL (increases the performance & shorter code)

  1. use shorthand operators
  2. use bitwise operators (don't use Math)
  3. use a,b,c... for your temp vars
  4. use the old syntax (while,for... not forEach)
  5. use the function arguments as placeholder (in some cases)
  6. remove unneccessary "{}","()",";",spaces,newlines
  7. Use the minifier

Now if a minifier can compress the code your doing it wrong.

No minifier can compress properly a bad code.

DIY

function myFunction(a,b,c){
 for(b=[],c={};a--;)b[a]=a,c[a]=a+1+'';
 return[b,c]
}

It does exactly the same thing as the codes above.

Performance

http://jsperf.com/diyminify

You always need to think what you need:

Before you say "No one would write code like the one below" go and check the first 10 questions in here ...

Here are some common examples I see every ten minutes.

Want a reusable condition

if(condition=='true'){
 var isTrue=true;
}else{
 var isTrue=false;
}
//same as
var isTrue=!!condition

Alert yes only if it exists

if(condition==true){
 var isTrue=true;
}else{
 var isTrue=false;
}
if(isTrue){
 alert('yes');
}
// The same as
!condition||alert('yes')
// If the condition is not true alert yes

Alert yes or no

if(condition==true){
 var isTrue=true;
}else{
 var isTrue=false;
}
if(isTrue){
 alert('yes');
}else{
 alert('no');
}
// The same as
alert(condition?'yes':'no')
// If the condition is true alert yes else no

Convert a number to a string or vice versa:

var a=10;
var b=a.toString();
var c=parseFloat(b)
// The same as
var a=10,b,c;
b=a+'';
c=b*1

// Shorter
var a=10;
a+='';// String
a*=1;// Number

Round a number

var a=10.3899845
var b=Math.round(a);
// The same as
var b=(a+.5)|0; // Numbers up to 10 decimal digits (32bit)

Floor a number

var a=10.3899845
var b=Math.floor(a);
// The same as
var b=a|0;//numbers up to 10 decimal digits (32bit)

switch case

switch(n)
{
case 1:
  alert('1');
  break;
case 2:
  alert('2');
  break;
default:
  alert('3');
}

// The same as
var a=[1,2];
alert(a[n-1]||3);

// The same as
var a={'1':1,'2':2};
alert(a[n]||3);

// Shorter
alert([1,2][n-1]||3);
// Or
alert([1,2][--n]||3);

try catch

if(a&&a[b]&&a[b][c]&&a[b][c][d]&&a[b][c][d][e]){
 console.log(a[b][c][d][e]);
}

// This is probably the only time you should use try catch
var x;
try{x=a.b.c.d.e}catch(e){}
!x||conole.log(x);

More if

if(a==1||a==3||a==5||a==8||a==9){
 console.log('yes')
}else{
 console.log('no');
}

console.log([1,3,5,8,9].indexOf(a)!=-1?'yes':'no');

But indexOf is slow. Read this: How do I check if an array includes a value in JavaScript?

Numbers

1000000000000
// The same as
1e12

var oneDayInMS=1000*60*60*24;
// The same as
var oneDayInMS=864e5;

var a=10;
a=1+a;
a=a*2;
// The same as
a=++a*2;

Some nice articles/sites I found about bitwise/shorthand:

http://mudcu.be/journal/2011/11/bitwise-gems-and-other-optimizations/

http://www.140byt.es/

http://www.jquery4u.com/javascript/shorthand-javascript-techniques/

There are also many jsperf sites showing the performance of shorthand & bitwise if you search with your favorite search engine.

I could go one for hours.. but I think it's enough for now.

If you have some questions, just ask.

And remember:

No minifier can compress properly bad code.

2 of 9
37

You could use one of the many available JavaScript minifiers.

  • YUI Compressor
  • Google closure compiler
  • Dean Edwards packer
  • JSMin
Find elsewhere
🌐
GitHub
github.com › oxc-project › oxc
oxc-project/oxc: ⚓ A collection of high-performance ...
5 days ago - Oxc is part of VoidZero's vision for a unified, high-performance toolchain for JavaScript. It powers Rolldown (Vite's future bundler) and enables the next generation of ultra-fast development tools that work seamlessly together. For more information, check out our website at oxc.rs. * Oxidation is the chemical process that creates rust · Rolldown and Nuxt use Oxc for parsing. Rolldown also uses Oxc for transformation and minification.
Starred by 20K users
Forked by 888 users
Languages   Rust 83.5% | JavaScript 9.9% | TypeScript 6.6% | Just 0.0% | Shell 0.0% | Vue 0.0%
🌐
DigitalOcean
digitalocean.com › community › tools › minify
JavaScript Minify Tool | DigitalOcean
Minify your JavaScript source code with our interactive web interface for Terser.
🌐
Cloudflare
cloudflare.com › learning › performance › why-minify-javascript-code
Why minify JavaScript code? | Cloudflare
... Minification, also known as minimization, is the process of removing all unnecessary characters from JavaScript source code without altering its functionality. This includes the removal of whitespace, comments, and semicolons, along with ...
🌐
Reddit
reddit.com › r/javascript › what exactly is a minified javascript file?
r/javascript on Reddit: What exactly is a minified JavaScript file?
June 13, 2015 -

Is a minified JavaScript file simply a .js file where all the spaces and formatting have been removed?

Does this make the code execute faster or simply reduce the loading time because of file sizes? It just doesn't seem like it would make that much of a difference to me...

🌐
npm
npmjs.com › package › minify
minify - npm
2 weeks ago - Usage: minify [options] Options: ... version and exit --js minify javascript --css minify css --html minify html --auto auto detect format --fail-on-error exit with code 1 when minification fails · The bash command below creates a code snippet saved as hello.js....
      » npm install minify
    
Published   Mar 04, 2026
Version   15.2.0
Author   coderaiser
🌐
OneNine
onenine.com › home › web design › web management › visual branding
10 JavaScript Minification Tools for Faster Websites - OneNine
Below is a quick overview of the 10 best JavaScript minification tools to help you optimize your code efficiently: UglifyJS: Reliable, supports modern JS, integrates with build tools, and generates source maps.
🌐
Imperva
imperva.com › home › performance › minification
What is Minification | Why minify JS, HTML, CSS files | CDN Guide | Imperva
December 20, 2023 - Minification has become standard practice for page optimization. All major JavaScript library developers (bootstrap, JQuery, AngularJS, etc.) provide minified versions of their files for production deployments, usually denoted with a min.js name extension.
🌐
Modpagespeed
modpagespeed.com › doc › filter-js-minify
Minify JavaScript
This filter minifies JavaScript code, using an algorithm similar to that in Douglas Crockford's popular JSMin program. At present, the filter strips all comments and most whitespace.
🌐
CleanCSS
cleancss.com › javascript-minify
Minify Javascript - Online Compressor / Packer
Minify and compress your javascript for free to make your website faster and more efficient by improving load time.
🌐
GitHub
github.com › topics › minify-js
minify-js · GitHub Topics · GitHub
Plugin to minify JS , CSS and HTML for quicken page load times by reducing the size of js/css/html files.
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
JS & CSS Minifier (Minify) - Visual Studio Marketplace
January 26, 2022 - Extension for Visual Studio Code - Easily Minify ES5/ES6/ES7/ES8 and CSS. Supports minify on save, minify selection & custom configurations!
🌐
Google
developers.google.com › insights › minify resources (html, css, and javascript)
Minify Resources (HTML, CSS, and JavaScript) | PageSpeed Insights | Google for Developers
To minify JavaScript, try UglifyJS. The Closure Compiler is also very effective. You can create a build process that uses these tools to minify and rename the development files and save them to a production directory.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-minify-javascript
How to Minify JavaScript - GeeksforGeeks
July 23, 2025 - Enhanced User Experience: Reduced ... conversions. Manual Minification involves manually removing unnecessary characters from the JavaScript code using a text editor or IDE....
🌐
7 Eagles
the7eagles.com › home › technical seo › how to minify javascript for faster website performance?
The Ultimate Guide to Minify JavaScript
December 19, 2025 - The main objective of Minification is to minimize the file sizes which leads to faster website loading times, downloads and enhances website performance. In production environments, minified JavaScript files are commonly used to improve user ...
🌐
EasyRetro
easyretro.io › tools › js-minifier
JS Minifier (Simple and Easy) | EasyRetro
JS Minify is a technique used to reduce the size of JavaScript code.