views:

234

answers:

8

If your website has deflate/zip compression enabled is there any point to JavaScript minification?

My theory is that the difference between a compressed minified JavaScript file and a compressed unminified JavaScript file is negligible.

There are very few browsers left out there that don't support compression. I would imagine that some bots (spiders) might not support compression (I know of at least one) but they are unlikely to be "interested" in your JavaScript as they are unlikely to be executing JS and so shouldn't be downloading it.

+2  A: 

Having a file minified before you gzip it will make a slight difference in server performance, though I doubt that adds up to much. Minification will remove comments, which gzip/deflate will not, but other than that I'd say you're correct.

Of course, there's always IE6. In my experience, this browser is unreliable when it comes to gzipped anything other than text/html. It's almost to the point where it doesn't matter, though, as IE6 usage continues to drop.

Alex JL
It's *almost* to the point where it doesn't matter? I wish my clients took the same view. It's like the "a little bit pregnant" trope: as long as just one of your clients insists on IE6 support, you have to support it. I wish to hell Microsoft would force this issue somehow and get rid of it altogether.
Robusto
Sure, if you are doing client work it's up to them. If not, it's your discretion - recently only 2.5% of visitors to the sites I work with are using IE6. We still support it, but probably if we didn't, the effect would not be very large. I expect to stop worrying about it in about a year.
Alex JL
MS could help force the issue by offering free upgrades from old versions of InfoPath to InfoPath 2010. I see "InfoPath" in a lot of IE6 user-agent strings from the corporate world (via a site that still has around 30% IE6 <strike>losers</strike> users). Apparently, if you install IE7 with old InfoPath, things break.
Matt
+20  A: 

Let's just test it. I used jQuery 1.4.2 and gzip (without flags; -9 does not seem to make a significant difference) to get the following numbers.

  • Development: 163,855 bytes
  • Development, compressed: 45,994 bytes
  • Minified: 72,174 bytes
  • Minified, compressed: 24,565 bytes

So, in this particular case, minification makes the file nearly twice as small. Admittedly, the development release is full of comments. Let's strip those out, and see what happens:

  • Stripped: 131,155 bytes
  • Stripped, compressed: 32,914 bytes

That's still significantly larger than the minified version.

Thomas
+1  A: 

I tried to zip jquery-1.3.2 in both original and minified versions:

jquery-1.3.2.js      118 kb  ->  36 kb
jquery-1.3.2.min.js   56 kb  ->  20 kb

So, minifying before compressing does make a substantial diffence.

Guffa
That's interesting. Any speculation on why that might be, other than removal of comments?
Alex JL
Minification makes the code much more homogeneous, meaning that it can be better compressed. You have less "noise"(long variable names) and more of the same symbols `()[]{}` etc. that allows for a higher compression ratio, just as a file full of zeros would end up being small when compressed, than one with actual data(that's not all zeros etc.)
Ivo Wetzel
Sure, if you are using minification that renames variables that makes sense. Isn't that something more like Closure Compiler? I was thinking of the type that only removes whitespace and comments.
Alex JL
+1  A: 

I believe that a minified version will run faster. Variable are now 1-2 glyphs long, so parsing is quicker, whitespace and comments are a non-issue. Of course, you would need to design a test in order to be able to actually tell any difference.

Compression for the mobile platform has pros and cons. Yes, it downloads a bit faster, but decompressing does eat up battery life.

--Dave

the Hampster
A: 

Every byte counts. More you save, better it is.

Ashit Vora
A: 

You could also use a JavaScript compressor/packer that uses something like base-62 encoding (for example, this).

It can turn 72174 bytes (jquery-1.4.2.min.js) into about 50640 bytes. However, gzipping it further won't improve the compression compared with a direct gzipping of the minified file (24K too).

(You may also need to preserve the licence headers if you're using a compressor/packer, about 400 bytes in this example).

Bruno
Don't use a JavaScript compressor; the file to be transferred will have approximately the same size, but it loads slower, as decompression within JavaScript is *way* slower than decompression using native GZip. See http://www.ericmmartin.com/comparison-of-javascript-compression-methods/
Marcel Korpel
I was just considering the size, but you're right.
Bruno
A: 

Check out Yahoo's Developer site - http://developer.yahoo.com/performance/rules.html - for some explanation about why minifying along with compression is good. Also, check out anything by Steve Souders (High Performance Web Sites -- awesome site and book!).

I'd avoid obfuscation unless you really want to squeeze as much as you can out of your scripts. Obfuscation, depending on how you wrote your JavaScript, could lead to errors. You may be better off just minifying and getting 80-90% of the way there.

Good luck!

David Hoerster
A: 

Keep in mind that minification actually discards information. Whitespace/comments/lengthy variable names/etc... are completely discarded (and cannot be recovered).

Server compression, on the other hand, needs to be lossless, so it can't discard any information. It can only compress it.

Server compression therefore can't (theoretically) reach the same levels of compression that minificaiton can (theoretically) achieve.

Remember, though that while theory and practice are theoretically the same, in practice they never are. :-)

loneboat