Which javascript minification library produces better results?


Answers

A great way to compare the best compressors is The JavaScript CompressorRater by Arthur Blake.

What you're usually interested in is the size after compressing with GZIP (you should configure your web server to perform the compression).

The best results are usually from the YUI Compressor or Dojo ShrinkSafe. The differences were so small that after a while I stopped comparing and I just use the YUI Compressor.

EDIT: since the original time this question was asked, 2 new minifiers have been released. They're both usually at least as good as, if not better than, the YUI Compressor.

EDIT 2:

  • UglifyJS, chosen by the jQuery team for the official 1.5 release
Question

Between Yahoo! UI Compressor, Dean Edwards Packer and jsmin, which produces better results, both in terms of resulting footprint and fewer errors when obfuscating.




It is good to comment code, but when javascript is used in production it should be as compact as possible. Your best bet is to minify the code for production. Search for "minify java script" on the internet.




Ways to compress/minify javascript files

Yahoo's compressor, for a combination of safety, and decent compression.

It uses a real JavaScript parser, rather than using a set of regular expressions. It is able to do the same kind of variable renaming that Packer can, but is much safer. It won't break if you forget to put a semicolon at the end of a function definition.

Packer does have an encoding feature that generates an even smaller file. However, it's only effective if your web server does not support gzip or deflate compression. With gzip / deflate, YUI and Packer generate files that are about the same size.




can we hide javascript comment in page when it renders ?

Look at YUI to minimise your javascript. You will need to do some post-processing using a tool like that when you deploy to remove comments. Otherwise, you're just sticking the same file up and people can see what you do (just like html or anything else that is served to the user directly).

Look at this question for a good answer:

YUI remove javascript comments

PS I'm assuming your javascript is in a .js or a .html file, otherwise (if in an asp, aspx or php file) the other answers about using server side comments are the best way to do it.

Still.. your javascript should be in a js file. It's much easier to manage :)




Changing the server settings to use gzip compression, then you get compression on any text file, javascript, html, etc. You also won't get the decompression lag that you get with compressed javascript on each page load.






Links



Tags