javascript - Google Closure 编译器不压缩字符串值?

标签 javascript compression minify google-closure-compiler yui-compressor

有这样的东西:

(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结果为 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 compressor结果为 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 中也压缩那些字符串变量吗? 为什么不这样做? 是因为在性能方面有更好的结果吗?

最佳答案

这包含在 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.

关于javascript - Google Closure 编译器不压缩字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28994591/

相关文章:

javascript - JQuery 数据表和 Bootstrap 3 日期选择器

c++ - WebP 无损格式概述

javascript - 精简 Javascript 代码和 css 代码的最佳应用

php - 将所有 css 加载为单个 http 请求和缩小形式?

javascript - 在 React js 中使用 Reactstrap 从多个选择下拉列表中动态选择选项

JavaScript 超时触发 3 次而不是一次(clearTimeout 不起作用?)

javascript - 使用 d3.js 选择包含的 DOM 树中的一个元素

android - 使用 android MediaCodec api 压缩视频

Ruby Net::HTTP POST 发送数据 gzip 压缩

javascript - 在 Rails 上使用 angularjs 和 ruby​​ 时出现未知的 Provider e-provider 错误