Gzip versus minify

Very simple to test. I took your js, put them in different files and ran gzip -9 on them. Here's the result. This was done on a WinXP machine running Cygwin and gzip 1.3.12.

-rwx------  1 xxxxxxxx mkgroup-l-d     88 Apr 30 09:17 expanded.js.gz

-rwx------  1 xxxxxxxx mkgroup-l-d     81 Apr 30 09:18 minified.js.gz

Here's a further test using a real-world JS example. The source file is "common.js" The original file size is 73134 bytes. Minified, it came to 26232 bytes.

Original file:

-rwxrwxrwx 1 xxxxxxxx mkgroup-l-d 73134 Apr 13 11:41 common.js

Minified file:

-rwxr-xr-x 1 xxxxxxxx mkgroup-l-d 26232 Apr 30 10:39 common-min.js

Original file gzipped with -9 option (same version as above):

-rwxrwxrwx 1 xxxxxxxx mkgroup-l-d 12402 Apr 13 11:41 common.js.gz

Minified file gzipped with -9 option (same version as above):

-rwxr-xr-x 1 xxxxxxxx mkgroup-l-d  5608 Apr 30 10:39 common-min.js.gz

As you can see, there is a definite difference between the various methods. The best bet is to both minify as well as gzip them.


You're right.

It's not the same to minify than gzipping (they would be called the same if that was the case). For example, it's not the same to gzip this:

var myIncrediblyLongNameForThisVariableThatDoesNothingButTakeUpSpace = null;

Than minify to end up with something like:

var a = null;

Of course, I'd say the best approach in most cases it to minify FIRST then Gzip, than just minifying or gzipping, although depending on the code sometimes just minifying or gzipping will give you better results than doing both.


Watch out when testing this: those two snippets of CSS are trivially small, so they don't benefit from GZIP compression - the addition of GZIP's small header and footer (about 20 bytes overhead) alone will lose the gains made. In reality you would not have a CSS file this small and be concerned about compressing it.

minify+gzip compresses more than just gzip

The answer to the original question is, yes, minify + gzip will gain a significant amount more compression than just gzip. This is true for any non-trivial example (ie any useful JS or CSS code that is more than a few hundred bytes).

For examples of this in effect, grab Jquery source code which is available minified and uncompressed, compress them both with gzip and take a look.

It's worth noting that Javascript benefits a lot more from minification than well-optimised CSS, but there is still a benefit.

Reasoning:

GZIP compression is lossless. This means that it has to store all text, including the exact whitespace, comments, long variable names and so on, so they can be perfectly reproduced later. On the other hand, minification is lossy. If you minify your code, you are removing much of this information from your code, leaving less that GZIP needs to preserve.

  • Minification discards unnecessarily whitespace, leaving spaces only where necessary for syntactical reasons.
  • Minification removes comments.
  • Code minification may replace identifier names with shorter names where there would be no side-effects.
  • Code minification may make trivial 'compiler optimizations' to the code which are only possible by actually parsing the code
  • CSS minification may eliminate redundant rules or combine rules which have the same selector.

Here are the results of a test I did a while back, using a "real life" CSS file from my website. CSS Optimiser is used for minification. The standard Linux archive app that comes with Ubuntu was used for Gzipping.

Original: 28,781 bytes
Minified: 22,242 bytes
Gzipped: 6,969 bytes
Min+Gzip: 5,990 bytes

My personal opinion is to go for Gzipping first, since that obviously makes the biggest difference. As for minification, it depends on how you work. You'd have to keep the original CSS file in order to make edits further down the line. If it doesn't bother you to minify it after every change then go for it.

(Note: there are other solutions, such as running it through a minifier "on-demand" when serving the file, and caching it in the filesystem.)