当使用YUI压缩器,我应该结合然后缩小,或缩小然后结合? [英] When using the YUI compressor, should I combine then minify, or minify then combine?

查看:211
本文介绍了当使用YUI压缩器,我应该结合然后缩小,或缩小然后结合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过一些人说,如果你结合然后缩小可能会有问题,并且你应该总是缩小然后结合(例如参见这个问题。)然而,我从来没有见过这些问题是什么或自己遇到的例子。我也找不到任何官方从YUI devs说明最好的做法是什么。

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.

推荐答案

由于方式压缩算法

目前最流行的压缩算法是GZIP。它是如何工作的,它试图引用字符(或字符串)从他们的最后一次出现的位置,并定义这些字符可以重复多少。

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.

让我们假设你想要压缩的字符串是: AABAB

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

下面是:

[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. 


$ b b

缩小



知道,如果我们重新使用相同的字符,我们可以看到对算法来说很重要,例如函数参数名称 - 这正是缩小的功能(请参阅:闭包编译器)。如果给出这两个函数:

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);
}

输出版本如下:

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.

其他来源:

Google开发人员关于压缩方法的视频

解析GZIP格式

这篇关于当使用YUI压缩器,我应该结合然后缩小,或缩小然后结合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆