Google Closure编译器不压缩字符串值? [英] Google Closure compiler not compressing string values?

查看:167
本文介绍了Google Closure编译器不压缩字符串值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有这样的:

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

关闭简单模式会导致 200个字节

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

YUI压缩器会导致 169字节

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

有没有办法在Closure中压缩这些字符串变量?
为什么不是这样做呢?
是因为在性能方面效果更好吗?

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?

推荐答案

这是涵盖的是Closure编译器常见问题。 https://github.com/google/closure-compiler/wiki/FAQ#closure-compiler-inlined-all-my-strings-which-made-my-code -size-bigger-why-did-it-do-

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假定您使用gzip压缩。如果你
不,你应该。配置你的服务器gzip你的代码是一个
的最有效和最简单的优化,你可能
。 gzip算法通过尝试以
中的字节序列进行别名的最佳方式来工作。手动差分字符串几乎总是使
压缩的代码大小更大,因为它破坏gzip自己的算法
的别名。所以Closure Compiler会(几乎)总是在你的
字符串内嵌,因为这将使你的压缩代码
更小。

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.

这篇关于Google Closure编译器不压缩字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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