When using the YUI compressor, should I combine then minify, or minify then combine?
Asked Answered
I

2

5

I've read some people saying there can be issues if you combine and then minify, and that you should always minify then combine (See for example the comments on this question.) However, I've never seen an example of what these issues are or encountered them myself. I also can't find anything official from the YUI devs stating what the best practice is.

Indisposition answered 2/11, 2013 at 20:8 Comment(0)
F
10

Due to the way compression algorithms work, combining and then minifying should give us the best results.

GZIP compression

Currently the most popular compressing algorithm is GZIP. How it works is that it tries to reference the position of a character (or a chain of characters) from their last occurrence, and define how many of these characters can be repeated.

Let's assume the string you'd want to compress is: AABAB

It would be broken by the algorithm down into:

[0,0]A  - Last occurrence of A was 0 characters ago, and its length was 0
[1,1]A  - Last occurrence of A was 1 characters ago, and its length was 1 char
[0,0]B  - Last occurrence of B was 0 characters ago, and its length was 0
[2,2]AB - Here comes the interesting part. We will only reference the set of
          characters, not occurrence of each character. The last occurrence of AB
          was 2 characters ago, and the length of this set of characters is 2. 

Minification

Knowing that, we can see that it matters a lot to the algorithm if we're re-using the same characters as, for example, function argument names - and that's precisely what minification does (see: closure compiler). If given these two functions:

function hello(name, surname) {
    alert('Welcome '+ name + ' ' + surname);
}

function logout( id ) {
    alert('Logged out '+ id);
}

The output version would come as:

function hello(a,b){alert("Welcome "+a+" "+b)}
function logout(a){alert("Logged out "+a)};

Which would allow the algorithm to be more efficient. Now this is a very simplified example, however in bigger chunks of code, where the variables or even function names can be replaced by their minified versions, the order of doing things will start to matter.

Some more sources:

Google developers video about compression methods

Dissecting the GZIP format

Fair answered 5/11, 2013 at 21:36 Comment(0)
I
3

Combine then minify. If the same value is found in more then one file then all occurrences would be replaced by the minified representation. If minified first then this would not be possible, resulting in a not quite as optimized version.

Incisure answered 5/11, 2013 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.