2011-11-09 23 views
1

我正在使用Google的Closure编译器来缩小我的JS。我的代码中有几个地方有重复的字符串,例如防止Closure编译器重复字符串

(function($){ 


$('.bat').append('<p>the red car has a fantastically wonderfully awe inspiringly world class engine</p><p>the blue car has a fantastically wonderfully awe inspiringly world class stereo</p><p>the green car has a fantastically wonderfully awe inspiringly world class horn</p>') 

})(jQuery); 

编译器并没有最大限度地减少冗余(可以预期),所以我做到了我自己的“预编译的”代码:

(function($){ 

    var ch = ' car has a fantastically wonderfully awe inspiringly world class '; 
    $('.bat').append('<p>the red'+ch+'engine</p><p>the blue'+ch+'stereo</p><p>the green'+ch+'horn</p>') 

})(jQuery); 

但是当我运行,通过编译器将其反转我压缩,这导致更多的字符。它输出:

(function(a){a(".bat").append("<p>the red car has a fantastically wonderfully awe inspiringly world class engine</p><p>the blue car has a fantastically wonderfully awe inspiringly world class stereo</p><p>the green car has a fantastically wonderfully awe inspiringly world class horn</p>")})(jQuery); 

有没有办法来防止这种情况?任何想法为什么这样做?它是运行时间性能改进吗?

感谢

+0

据我所知,关闭是做正确的事。变量'ch'不在外部使用,它通过内联来节省字节(并提高性能)。你的做法似乎没有任何解决方法来避免“重复的字符串”。另外,请注意,虽然重复的字符串在存储上看起来效率低下,但它们不会影响gzip的大小。 –

回答