Best JavaScript compressor

JavascriptCompression

Javascript Problem Overview


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)

Javascript Solutions


Solution 1 - Javascript

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. ;-)

Solution 2 - Javascript

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 http://www.julienlecomte.net/blog/2007/08/13/"> were as follow

Basically http://dean.edwards.name/packer/">Packer</a> 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) http://www.julienlecomte.net/yuicompressor/">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

Solution 3 - Javascript

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.

Solution 4 - Javascript

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.

Solution 5 - Javascript

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

Solution 6 - Javascript

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.

Solution 7 - Javascript

JSMin is another one.

Solution 8 - Javascript

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

Solution 9 - Javascript

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

Solution 10 - Javascript

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

[1]: http://brilaps.com/index.php?content=byuic "Here" [2]: http://brilaps.com/index.php?content=byuic "Byuic"

Solution 11 - Javascript

bananascript.com used to give me best results.

Solution 12 - Javascript

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.

Solution 13 - Javascript

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

[1]: http://www.iteral.com/jscrush/ "JS Crush"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRon HarlevView Question on Stackoverflow
Solution 1 - JavascriptmishooView Answer on Stackoverflow
Solution 2 - JavascriptPatView Answer on Stackoverflow
Solution 3 - JavascriptkamensView Answer on Stackoverflow
Solution 4 - JavascriptJason BuntingView Answer on Stackoverflow
Solution 5 - JavascriptchakritView Answer on Stackoverflow
Solution 6 - JavascriptjcoglanView Answer on Stackoverflow
Solution 7 - JavascriptMark BiekView Answer on Stackoverflow
Solution 8 - JavascriptwotoView Answer on Stackoverflow
Solution 9 - JavascriptjuanView Answer on Stackoverflow
Solution 10 - JavascriptjimgView Answer on Stackoverflow
Solution 11 - JavascriptPaweł HajdanView Answer on Stackoverflow
Solution 12 - JavascriptMicTechView Answer on Stackoverflow
Solution 13 - JavascriptAlexView Answer on Stackoverflow