
YUICompressor - improving compression
Having optimized the code of my library a bit, I suddenly noticed that it began to compress much worse. For compression I use YUICompressor . The firm belief that optimization should still be for the good, not the detriment, I decided to figure out what was the matter. And here it turns out: YUICompressor does not properly digest window.eval inside (function () {...}) (). Now an example of poor compressibility:
after compression
As you can see, the variables TOPLONGNAME and LOCALLONGNAME are not optimized.
How to win this? Very simple! Replace window.eval with window ['eval'].
Example:
After compression:
Maybe someone will come in handy ... In any case, I wrote to the YUICompressor developer about this bug, we are waiting for a fix.
(function () {
var TOPLONGNAME = 'a';
var obj = {
test: function () {
var LOCALLONGNAME = 'b';
var res = window.eval (function () {
alert (TOPLONGNAME + LOCALLONGNAME);
});
res ();
}
}
obj.test ();
}) ()
after compression
(function () {var TOPLONGNAME = "a"; var obj = {test: function () {var LOCALLONGNAME = "b"; var res = window.eval (function () {alert (TOPLONGNAME + LOCALLONGNAME)}); res ()}}; obj.test ()}) ();
As you can see, the variables TOPLONGNAME and LOCALLONGNAME are not optimized.
How to win this? Very simple! Replace window.eval with window ['eval'].
Example:
(function () {
var TOPLONGNAME = 'a';
var obj = {
test: function () {
var LOCALLONGNAME = 'b';
var res = window ['eval'] (function () {
alert (TOPLONGNAME + LOCALLONGNAME);
});
res ();
}
}
obj.test ();
}) ()
After compression:
(function () {var A = "a"; var B = {test: function () {var D = "b"; var C = window ["eval"] (function () {alert (A + D)} ); C ()}}; B.test ()}) ();
Maybe someone will come in handy ... In any case, I wrote to the YUICompressor developer about this bug, we are waiting for a fix.