Parris
Parris

Reputation: 18408

why does minification + gzip make file sizes smaller than just gzip

As I understand it:
- Minification crawls through your source code and replaces code it can for shorter variables, removes white space, makes a few code replacement optimizations (sometimes).
- gzip dedupes your code

Wouldn't gzip + minfication be at odds with each other? It stands to reason that after a minification things would be more difficult to dedupe.

Somehow though gzip + minification results in a smaller file size.

Why?

Upvotes: 3

Views: 152

Answers (2)

thejh
thejh

Reputation: 45568

Minification is allowed to permanently alter your code. It can e.g. simply replace a variable name foobar with a variable name a without needing to remember how to undo the minification, and it can just throw away unneeded whitespace. However, it can't reduce the space wasted because all characters are encoded using the same length or because the same arguments are used in two method calls – after all, the minified version still needs to be valid code.

Compression e.g. using gzip is allowed to produce output that's not valid javascript anymore. Therefore, gzip can e.g. encode characters with different lengths so that curly braces consume less than eight bits, and it can compress common parts in the code. However, because it needs to be able to restore the original code, it can't just rename your variables to something shorter.

Upvotes: 3

Patrick Hofman
Patrick Hofman

Reputation: 156948

That is because there is less to keep.

Minification will also remove line ends when possible, brackets and other redundant code. Code that is redundant doesn't have to be zipped. And that's why they are both less then just one of the two.

Upvotes: 0

Related Questions