Take the 2-minute tour ×

JQuery has two versions for download, one is Production (19KB, Minified and Gzipped), and the other is Development (120KB, Uncompressed Code).

Now the compact 19kb version, if you download it, you will see is still a javascript executable code. How did they compactify it? And how can I 'minify' my code like that too?

share|improve this question
Especially, is there any online utility which lets me do this? –  KalEl Nov 15 '09 at 12:12
I stumbled upon this old post with the same questions, so good question! Some good basic info: thiscouldbeuseful.com/2012/09/minified-js-for-beginners.html. –  Aries51 Sep 25 '12 at 22:19

9 Answers 9

up vote 64 down vote accepted

There’s a nice comparison of JavaScript compressors you should take a look at.

share|improve this answer
Thanks. When I click the view button, it shows something like my original code (for any of the compressors). Is it decoding and then showing it? Otherwise is there also any way I can see the compressed code too? –  KalEl Nov 15 '09 at 12:31
@KalEl: Click on View in the Result column to see the output. –  Gumbo Nov 15 '09 at 12:37

DIY Minification

No minifier can compress properly a bad code.

In this example i just wanna 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 caniuse.com, almost everything works on every browser (also ie10 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 100kb jquery script everythime?how big is your uncompressed code? 5-6kb..? 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++){
         var myString = myCounter.toString()
         myObject[ myString ] = ( myCounter + 1 ).toString();
    var myContainer = new Array();
    myContainer[0] = myArray2;
    myContainer[1] = myObject;
    return myContainer;

Here iss 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++){
  var o=a.toString();
 var i=new Array;
 return i[0]=e,i[1]=t,i

But are all those vars , ifs, loops & 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.


function myFunction(a,b,c){

It does exactly the same thing as the codes above.



You always need to think what you need:

Before you say "Noone 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

 var isTrue=true;
 var isTrue=false;
//same as
var isTrue=!!condition

Alert yes only if it exists

 var isTrue=true;
 var isTrue=false;
//same as
//if the condition is not true alert yes

Alert yes or no

 var isTrue=true;
 var isTrue=false;
//same as
//if the condition is true alert yes else no

Convert a number to a string or viceversa

var a=10;
var b=a.toString();
var c=parseFloat(b)
//same as
var a=10,b,c;

var a=10;

Round a number

var a=10.3899845
var b=Math.round(a);
//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);
//same as
var b=a|0;//numbers up to 10 decimal digits (32bit)

switch case

case 1:
case 2:

//same as
var a=[1,2];

//same as
var a={'1':1,'2':2};

try catch


//this is probably the onle time you should use try catch
var x;

more if




//same as

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

var a=10;
//same as

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




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

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 a bad code.

share|improve this answer
It's incredible that you've received so little attention for this fantastic answer. –  rosscowar Jul 30 at 8:15
There is hardly any reason to hand-minify code. Write code that is easily understood by other developers (or you, 10 months later). Yes, simpler is better. Use a minified in an automated build process that preserves the original. In almost every case, any speed gains from hand-optimization are far, far outweighed by the cost of developers deciphering minified code. –  alttag Aug 12 at 0:38
depends what you are doing. if your working with animations/canvas, huge datasets & file manipolation for example, a fast code is very important, especially on mobile devices...the point is, for some developers it's hard to read.yeah... i write code since the pentium 2 .. so probably 1998, i can read the code and in my experience i have less code to check for errors.And about the speed.. mh, your wrong. Performance increases using bitwise/and shorthand in complex functions is insane.expecially testing on various devices/browsers.use google shorthandbitwise javascript and you find many examples –  cocco Aug 12 at 1:21
anyway there is jqeury, they made it for those ppl who don't wanna try to learn javascript properly.Easy and compatible with most browsers. –  cocco Aug 12 at 1:36
Great answer. Minifiers only go so far -- real performance gain is achieved with proper logic and proper code. –  aug Sep 15 at 22:19

There is an online tool that also minifies javascript,html,php and css code.


share|improve this answer

I recently needed to perform the same task. While the compressors listed at The JavaScript CompressorRater do a great job and the tool is very useful, the compressors were not playing nice with some jQuery code I am using ($.getScript and jQuery.fn checks). Even the Google Closure Compressor choked on the same lines. While I could have eventually ironed out the kinks it was far to much squinting to do constantly.

The one that finally worked without issue was UglifyJS (thanks @Aries51), and the compression was only slightly less than all the others. And similar to Google it has a HTTP API. Packer is also nice and has language implementation in Perl, PHP, and .NET.

share|improve this answer

Another option slightly better than YUI Compressor.. http://openwaf-js-mini.appspot.com/

share|improve this answer
Thanks for posting your answer! Please be sure to read the FAQ on Self-Promotion carefully. Also note that it is required that you post a disclaimer every time you link to your own site/product. –  Andrew Barber Dec 3 '12 at 17:57

There is also free Minifier from Microsoft on codeplex. Microsoft Ajax Minifier 1.1 : http://ajaxmin.codeplex.com/

It's easy to work with and does a good job.


share|improve this answer

You could use one of the many available javascript minifiers.

share|improve this answer
I understand that YUI Compressor is deprecated in favor of UglifyJS (demo). –  MartyIX Nov 29 at 10:41

Google just made available a javascript compiler that can minify your code, elimiated dead code branches and more optimizations.

google javascript compiler


share|improve this answer


share|improve this answer

Your Answer


