Google Closure compiler not compressing string values?

房东的猫 提交于 2019-12-12 15:16:05

问题


Having something like this:

(function ($, window, document, undefined) {
    'use strict';
    $.fn.demo = function (options) {

        var active = "active";
        var section = ".bb-demo";

        $(section).addClass(active);
        $(section).addClass(active);
        $(section).addClass(active);
        $(section).addClass(active);

    };
})(jQuery, window, document);

Closure Simple mode results in 200 bytes:

(function(a,b,c,d){a.fn.demo=function(b){a(".bb-demo").addClass("active");a(".bb-demo").addClass("active");a(".bb-demo").addClass("active");a(".bb-demo").addClass("active")}})(jQuery,window,document);

While YUI compressor results in 169 bytes:

(function(c,b,a,d){c.fn.demo=function(e){var g="active";var f=".bb-demo";c(f).addClass(g);c(f).addClass(g);c(f).addClass(g);c(f).addClass(g)}})(jQuery,window,document);

Isn't there a way to compress those string variables in Closure as well? why isn't it doing it? Is it because of better results in terms of performance?


回答1:


This is covered is Closure Compiler FAQ. 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

Closure Compiler assumes that you are using gzip compression. If you do not, you should. Configuring your server to gzip your code is one of the most effective and easiest optimizations that you can possibly do. The gzip algorithm works by trying to alias sequences of bytes in an optimal way. Aliasing strings manually almost always makes the compressed code size bigger, because it subverts gzip's own algorithm for aliasing. So Closure Compiler will (almost) always inline your strings when it can, because that will make your compressed code smaller.



来源:https://stackoverflow.com/questions/28994591/google-closure-compiler-not-compressing-string-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!