为什么 google 闭包编译不在变量中保留重复使用的索引?

Why doesn't the google closure compile keep reused indexes in variables?

This MWE 展示了 google 闭包编译器如何将短的 obj[keyA] 换成较长的 obj["some-very-long-key"]:

输入javascript:

var foo = new function() {
  var keyA = 'some-very-long-key';
  var keyB = 'another-key';
  this.bar = function() {
    obj[keyA] = {};
  }
  this.baz = function(data) {
    obj[keyA][keyB] = data;
  }
}();

Google 闭包编译器输出:

var foo = new function() {
  this.bar = function() {
    obj["some-very-long-key"] = {};
  };
  this.baz = function(a) {
    obj["some-very-long-key"]["another-key"] = a;
  };
};

如果我去掉包装函数,it works as I expected it to:

输入javascript:

var keyA = 'some-very-long-key';
var keyB = 'another-key';
function bar() {
  obj[keyA] = {};
}
function baz(data) {
  obj[keyA][keyB] = data;
}

Google 闭包编译器输出:

var keyA = "some-very-long-key", keyB = "another-key";
function bar() {
  obj[keyA] = {};
}
function baz(a) {
  obj[keyA][keyB] = a;
}
;

因为我在我的项目中经常使用长密钥,如果 google 闭包编译器将字符串文字保存在变量中,代码会变得比它可能的更大。


更新 1: 我知道我想要的结果可能会稍微差一点,但我宁愿使用更短的代码。

因为 gzip - 它通常会使压缩后的文件变小。甚至 in the FAQ