简体   繁体   English

JavaScript缩小字符串文字?

[英]JavaScript minification of string literals?

I was looking over some of our JavaScript compression and noticed that no strings that aren't object property names are minified into variables. 我正在查看一些JavaScript压缩,并注意到没有对象属性名称的字符串被缩小为变量。

For example, let's say I have these two pieces of code in my script: 例如,假设我的脚本中有这两段代码:

alert("Wrong answer");
alert("Wrong answer buddy");

The minification we get from YUI and Closure, if I'm not mistaken, is: 如果我没弄错的话,我们从YUI和Closure得到的缩小是:

alert("Wrong answer");alert("Wrong answer buddy") ; alert("Wrong answer");alert("Wrong answer buddy") ;

I would think the resulting minification would look like this: 我认为最终的缩小将如下所示:

var a="Wrong answer";alert(a);alert(a+" buddy");

Do any minification tools do this? 有没有缩小工具呢? What's stopping our tools from doing this? 什么阻止我们的工具这样做?

gzip compression will reduce identical strings to a byte or two. gzip压缩会将相同的字符串减少到一个或两个字节。 Use that and it's a non-issue. 使用它,这是一个非问题。 :) :)

If I were to hazard a guess, I would say that it is because the compiler cannot tell when it would be more optimal to break out your strings into variables and concatenate them. 如果我冒险猜测,我会说这是因为编译器无法判断何时将字符串分解为变量并将它们连接起来更为理想。 (And, as such things go, it falls under the heading of micro-optimization in the first place.) (而且,就这些事情而言,它首先属于微优化的标题。)

For example, the following code: 例如,以下代码:

if (test_case==="He fell down a well") {
    var i=my_object_array.length-1;
    while(i>=0) {
        my_object_array[i].say("He fell down a well did he Lassie?");
        i--;
    }
}

Might be re-rendered by your theoretical compiler as: 可能会被理论编译器重新渲染为:

var x="He fell down a well";
if (a===x) {
    var i=b.length-1;
    while(i>=0) {
        b[i].say(x+" did he Lassie?");
        i--;
    }
}

... which would of course, increase the time it takes for the while loop to complete its work. ...当然会增加 while循环完成其工作所需的时间。

Of course, a slightly more intelligent compiler might recognize that trap and optimize still further: 当然,稍微更智能的编译器可能会识别陷阱并进一步优化:

var x="He fell down a well";
var x1=x+" did he Lassie?";
if (a===x) {
    var i=b.length-1;
    while(i>=0) {
        b[i].say(x1);
        i--;
    }
}

Either way though, a good javascript compiler, to my mind, optimizes the code first for performance , and only secondarily for character count. 无论哪种方式,一个好的javascript编译器,在我看来,首先优化代码的性能 ,其次只是字符数。 As this is an optimization primarily for character count improvement I think that there probably simply has not been enough demand for it to warrant the time of the people who maintain Closure and the YUI Compressor. 由于这是一个主要用于字符数改进的优化,我认为可能根本没有足够的需求来保证维护Closure和YUI Compressor的人的时间。

The compression tools have their limits and you found one. 压缩工具有其局限性,您找到了一个。 You should create your own variables for the strings, then the tools will compress the variable names. 您应该为字符串创建自己的变量,然后工具将压缩变量名称。

Eg
var msg_wrong = "Wrong answer",
    msg_very_wrong = msg_wrong + "!!!";
alert (msg_wrong);
alert (msg_very_wrong);


// "msg_wrong" and "msg_very_wrong" variable names will be compressed

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

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