简体   繁体   English

当使用YUI压缩器时,我应该组合然后缩小,还是缩小然后合并?

[英]When using the YUI compressor, should I combine then minify, or minify then combine?

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. 我也找不到YUI开发人员的任何官方说明最佳做法。

Due to the way compression algorithms work, combining and then minifying should give us the best results. 由于压缩算法的工作方式,组合然后缩小应该给我们最好的结果。

GZIP compression GZIP压缩

Currently the most popular compressing algorithm is GZIP. 目前最流行的压缩算法是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 我们假设您要压缩的字符串是: 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 Google开发者关于压缩方法的视频

Dissecting the GZIP format 解析GZIP格式

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. 如果首先缩小,那么这将是不可能的,导致不完全优化的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM