views:

1605

answers:

3

Duplicate:


So far I have seen these tools to compress javascript files

On Packer page there is a section "Packer versus JSMin" which says that JSMin can be more efficient than Packer. On Yahoo Compressor page it states that Yahoo compressor is more efficient than JSMin, so it looks like Yahoo compressor might be a best candidate.

What is the best option for compressing javascript files?

+2  A: 

I use YUICompressor exclusively because it intelligently minifies removing whitespace and reducing internal variables down whilst maintaining external refs properly and has yet to break my code and it also does CSS to boot!

After that we serve up on a GZip HTTP connection and voila!

Lloyd
A: 

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.

Adam Luter
lol @ not getting decompression lag on compressed files. GZip is compression, the client will have to decompress it to use it. Minified javascript is not compressed, just it's footprint is reduced as much as possible.
Malfist
OK, if you are only doing minified (sorry I misread) then you're only hurting readability of the javascript, I could take or leave that.Gzip compression is still the best way to do this since it can apply to a host of files, and decompression by the client is not an issue anymore since it's happening in a low-level language not javascript -- and only happening on download, not on page load.
Adam Luter
Gzip compression is not the best way to do it. Gzip + minification is the best way. Decompression by the client still happens. Downloading a page is part of loading the page, after all, you can't view the page without downloading it first.
Malfist
Minification affects code readability still, so whether or not it's best depends on if you are willing to.Download versus page load is a big difference. Download only happens once -- usually -- page load happens on every page.A javascript file is probably on several pages. Every page load would cause that javascript file to decompress in javascript. However, decompression from a http download would only happen once.Even if it only appeared on one page, revisiting this page -- whether by back button or re-clicking -- would not incur http decompression again, only javascript.
Adam Luter
Also, the most important bit, in my opinion is that gzip compression affects all content, not just javascript.Lastly, minification is something you have to remember to do everytime you make a change (or setup a process that does it for you when you publish). Whereas gzip compression is fire and forget.But as you say, both will give you the smallest possible sized file, but I normally only find about a 10%-20% gain from minification on top of gzip. Therefore, I do not bother with it because of the combination of lost readability and manually having to do it on publishing.
Adam Luter
+4  A: 

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.

BlackAura