174

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

  • is easy to use
  • has a high compression rate
  • Produce reliable end results (doesn't mess up the code)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ron Harlev
  • 16,227
  • 24
  • 89
  • 132
  • 14
    Anyone knows the situation in 2011? – Dan Jan 31 '11 at 11:40
  • 4
    It is now 2012, and I think UglifyJS and Closure are the winners, I use UglifyJS and it usually beats the pants off everything else. – mkoistinen Apr 03 '12 at 20:50
  • I use http://htmlcompressor.com/compressor.html for my jQuery Mobile multi-page app. It takes an HTML file with – Anders Aug 20 '12 at 15:39
  • It's 2017 - What's the latest now? – Abhinav Singi Jun 01 '17 at 07:21
  • It's 2020. With a default "safe" configuration, UglifyJS marginally wins because it is more "safe." For advanced power-uses, Closure Compiler hands UglifyJS's ass to itself. Closure Compiler with ADVANCDED_OPTIMIZATIONS can do all sorts of tricks that help you optimize your workflow and your code at the same time. See https://stackoverflow.com/a/50355530/5601591 for examples of Closure Compiler's awesomeness (DISCLAIMER: I could not find a suitable blog post elsewhere, so I had to refer you to a post I wrote). – Jack G Mar 23 '20 at 23:44

13 Answers13

150

I recently released UglifyJS, a JavaScript compressor which is written in JavaScript (runs on the NodeJS Node.js platform, but it can be easily modified to run on any JavaScript engine, since it doesn't need any Node.js 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 foo.bar 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. ;-)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mishoo
  • 2,415
  • 1
  • 18
  • 12
126

Revisiting this question a few years later, UglifyJS, seems to be the best option as of now.

As stated below, it runs on the NodeJS platform, but can be easily modified to run on any JavaScript engine.

--- Old answer below---

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

Community
  • 1
  • 1
Pat
  • 36,282
  • 18
  • 72
  • 87
  • 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 Sep 17 '08 at 07:19
  • Also, try http://compressorrater.thruhere.net/ if you don't believe me. – Daniel James Sep 17 '08 at 07:22
  • 9
    Don't forget the downside to packer--decompression time. – Nosredna Jun 07 '09 at 15:57
  • 1
    watch out, google closure can sometimes be the worst compressor (output even larger than original) - it converts non-ascii characters in strings to `\uxxxx` literals by default.. use e.g. `--charset UTF-8` (if you're sure you let the browser know about it somehow) – mykhal Feb 15 '12 at 09:35
  • The ClosureCompiler output doesn't work for me. http://jscompress.com works – codenamezero May 28 '13 at 18:59
  • YUI Compression gziped files are bigger than Closure compression ones. [Check this](http://stackoverflow.com/questions/28994591/google-closure-compiler-not-compressing-string-values). – Alvaro Mar 12 '15 at 08:57
43

YUI Compressor is the way to go. It has a great compression rate, is well tested and is in use among many top sites, and, well, personally recommended by me.

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

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

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

Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
kamens
  • 11,910
  • 6
  • 45
  • 46
  • 5
    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 May 19 '10 at 14:46
  • Here's one benchmark: http://www.ericmmartin.com/comparison-of-javascript-compression-methods/ – Colonel Sponsz May 19 '10 at 15:26
  • Here is an online version of the compressor in case you don't want to deal with getting java running: http://refresh-sf.com/yui/ – Bryan Legend Apr 21 '11 at 21:40
  • Scripts compressed using packer don't need to be unpacked unless you check the Base62 encode option (which you shouldn't be doing since it's a layman's gzip - I'm sure most modern servers support gzip). There's no point in gzipping a base62 encoded file since there's no redundancy left to exploit. The newest version of packer (the final version) doesn't introduce bugs, doesn't have an unpacking overhead (as long as you don't base62 encode), and still achieves the most compression. Also there's now a command line version of packer. Just install it using npm as follows: `npm install packer`. (=D – Aadit M Shah Apr 19 '12 at 15:17
8

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
  • 58,249
  • 14
  • 102
  • 93
5

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

chakrit
  • 61,017
  • 25
  • 133
  • 162
4

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.

jcoglan
  • 2,108
  • 1
  • 15
  • 12
3

JSMin is another one.

Mark Biek
  • 146,731
  • 54
  • 156
  • 201
2

In searching silver bullet, found this question. For Ruby on Rails http://github.com/sstephenson/sprockets

woto
  • 2,893
  • 1
  • 29
  • 24
1

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

juan
  • 80,295
  • 52
  • 162
  • 195
1

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.

jimg
  • 484
  • 6
  • 12
1

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

Alex
  • 1,327
  • 2
  • 11
  • 20
1

bananascript.com used to give me best results.

Paweł Hajdan
  • 18,074
  • 9
  • 49
  • 65
1

KJScompress

http://opensource.seznam.cz/KJScompress/index.html

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.

MicTech
  • 42,457
  • 14
  • 62
  • 79