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.
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:
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.
© 2022 - 2024 — McMap. All rights reserved.