📜 ⬆️ ⬇️

YUICompressor - improve compression

Having slightly optimized the code of my library, I suddenly noticed that it began to shrink worse by an order of magnitude. For compression I use YUICompressor . A firm belief that optimization should still go for the benefit, and not to the detriment, I decided to figure out what was the matter. And that's what it turns out : YUICompressor does not digest window.eval badly inside (function () {...}) (). Now an example of poor compressibility:

( 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 it? 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 developer of YUICompressor about this bug, we are waiting for the correction.

Source: https://habr.com/ru/post/37583/


All Articles