What is the the best Javascript compressor available? I'm looking for a tool that:

  • Easy to use
  • High compression rate
  • Reliable end results (doesn't mess up the code)
+24  A: 

YUICompressor is the way to go...great compression rate, well tested and in use among many top sites, and, well, personally recommended by me.

I've used it for my projects without a single JS error or hiccup. And it has nice documentation.

I've never used its CSS compression capabilities, but they exist as well.

*Note: Although Dean Edwards's /packer/ achieves a better compression rate than YUICompressor, I ran into a few JS errors when using it. *

Packer looks good in terms of file size but it turns out that the time spent unpacking normally outweighs the time gain of transferring a smaller file through the tubes. Most of the actual browser benchmarks I've seen have it slower than the raw uncompressed files served with gzip in terms of time to execution in the browser.
Colonel Sponsz
Here's one benchmark:
Colonel Sponsz
+3  A: 

JSMin is another one.

Mark Biek
+3  A: 

Try JSMin, got C#, Java, C and other ports and readily available too.

+1  A: 

Here's the source code of an HttpHandler which does that, maybe it'll help you

Juan Manuel
+18  A: 

Google released Closure Compiler which seems to be generating the smallest files so far as seen here and here

Previous to that the various options were as follow

Basically Packer does a better job at initial compression , but if you are going to gzip the files before sending on the wire (which you should be doing) YUI Compressor gets the smallest final size.

The tests were done on jQuery code btw.

  • Original jQuery library 62,885 bytes , 19,758 bytes after gzip
  • jQuery minified with JSMin 36,391 bytes , 11,541 bytes after gzip
  • jQuery minified with Packer 21,557 bytes , 11,119 bytes after gzip
  • jQuery minified with the YUI Compressor 31,822 bytes , 10,818 bytes after gzip

@daniel james mentions in the comment compressorrater which shows Packer leading the chart in best compression, so I guess ymmv

Packer has an option to 'base62 encode' off - and for jQuery it compresses smaller than yui after gzip. This is because jquery uses 'eval' and 'with' which prevents 'safe' compressors from doing certain compressions, but packer ignores them. Not safe in general but jQuery is tested for Packer.
Daniel James
Also, try if you don't believe me.
Daniel James
Don't forget the downside to packer--decompression time.
Jason Bunting
+6  A: 

I use ShrinkSafe from the Dojo project - it is exceptional because it actually uses a JavaScript interpreter (Rhino) to deal with finding symbols in the code and understanding their scope, etc. which helps to ensure that the code will work when it comes out the other end, as opposed to a lot of compression tools which use regex to do the same (which is not as reliable).

I actually have an MSBuild task in a Web Deployment Project in my current Visual Studio solution that runs a script which in turn runs all of the solution's JS files through ShrinkSafe before we deploy and it works quite well.

EDIT: By the way, "best" is open to debate, since the criteria for "best" will vary depending on the needs of the project. Personally, I think ShrinkSafe is a good balance; for some people that think smallest size == best, it will be insufficient.

EDIT: It is worth noting that the YUI compressor also uses Rhino.

Jason Bunting
+1  A: 

Here is a YUI compressor script (Byuic) that finds all the js and css down a path and compresses /(optionally) obfuscates them. Nice to integrate into a build process.

+3  A: 

If you use Packer, just go far the 'shrink variables' option and gzip the resulting code. The base62 option is only for if your server cannot send gzipped files. Packer with 'shrink vars' achieves better compression the YUI, but can introduce bugs if you've skipped a semicolon somewhere.

base62 is basically a poor man's gzip, which is why gzipping base62-ed code gives you bigger files than gzipping shrink-var-ed code.

+1  A: used to give me best results.

+1  A: 


Kjscompress/csskompress is set of two applications (kjscompress a csscompress) to remove non-significant whitespaces and comments from files containing JavaScript and CSS. Both are command-line applications for GNU/Linux operating system.

+2  A: 

In searching silver bullet, found this question. For Ruby on Rails

+4  A: 

I recently released UglifyJS, a JS compressor which is written in JavaScript (runs on NodeJS platform, but it can be easily modified to run on any JS engine, since it doesn't need any Node internals). It's a lot faster than both YUI Compressor and Google Closure, it compresses better than YUI on all scripts I tested it on, and it's safer than Closure (knows to deal with "eval" or "with").

Other than whitespace removal, UglifyJS also does the following:

  • changes local variable names (usually to single characters)
  • joins consecutive var declarations
  • avoids inserting any unneeded brackets, parens and semicolons
  • optimizes IFs (removes "else" when it detects that it's not needed, transforms IFs into the &&, || or ?/: operators when possible, etc).
  • transforms foo["bar"] into where possible
  • removes quotes from keys in object literals, where possible
  • resolves simple expressions when this leads to smaller code (1+3*4 ==> 13)

PS: Oh, it can "beautify" as well. ;-)


Js Crush is a good compressor to use after you have minified.

Alex Nolan