防止闭包编译器重复字符串

发布于 2024-12-15 00:37:48 字数 1213 浏览 3 评论 0原文

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

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

有办法防止这种情况发生吗?知道为什么这样做吗?这是运行时性能的改进吗?

谢谢

I'm using Google's Closure compiler to shrink my JS. There are several places in my code where I have a repeated string, e.g.

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

The compiler wasn't minimizing that redundancy (to be expected) so I did it myself in the 'precompiled' code:

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

But when I run that through the compiler it reverses my compression, which results in more characters. It outputs:

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

Is there a way to prevent this? Any idea why it's being done? Is it a run-time performance improvement?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦一生花开无言 2024-12-22 00:37:48

此行为记录在此处:

https://github.com/google/closure-compiler/wiki/FAQ#closure-compiler-inlined-all-my-strings-which-made-my-code-size-bigger-why-did-it -do-that

但是,当我添加一个值得重复数据删除的非常大的字符串时,我用来避免的一种方法是将值包装在函数中:

const getCssStyleSheetText = () => "...";

并在需要文本时调用该函数。编译器在内联函数时使用不同的启发式方法,并且仅在估计函数会减少代码大小时才会内联函数。因此,如果返回字符串的函数被调用一次,则该函数将被内联,但如果被调用多次,则将被保留。

理想情况下,编译器在内联字符串方面会更加细致一些,但总的来说,它所采用的方法效果很好。

This behavior is documented here:

https://github.com/google/closure-compiler/wiki/FAQ#closure-compiler-inlined-all-my-strings-which-made-my-code-size-bigger-why-did-it-do-that

However, one approach I've used to avoid when I have add a very large string that is worth deduplicating is to wrap the value in a function:

const getCssStyleSheetText = () => "...";

and to call that function when I need the text. The compiler uses a different heuristic when inlining functions and will only inline a function if it estimates that it will reduce code size. Therefore a function returning a string will be inlined if it is called once but left alone if it is called many times.

Ideally, the compiler would be a little more nuanced about inlining strings but in general the approach it takes works out well enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文