Javascript minification logic where immutable properties are involved
A question on the minifying logic.
// Assumptions:
//com.example.dummy ns is available
//com.example.dummy.type is defined immutable -> Object.defineProperty
var test, test1, test2;
function execute_case(id) {
switch(id) {
case 0:
test = com.example.dummy.type;
break;
case 1:
test1 = com.example.dummy.type;
break;
case 2:
test2 = com.example.dummy.type;
break;
default:
console.log("default");
break;
}
}
the YUI compressor compresses it to,
com.example.dummy.type="Type";var test,test1,test2;function
execute_case(a){switch(a){case 0:test=com.example.dummy.type;break;case
1:test1=com.example.dummy.type;break;case
2:test2=com.example.dummy.type;break;default:console.log("default");break}};
Wouldn't it be logical for the compressor do the following as an
optimization?
var test, test1, test2;
var st = com.example.dummy.type;
function execute_case(id) {
switch(id) {
case 0:
test = st;
case 1:
test1 = st;
break;
case 2:
test2 = st;
break;
default:
console.log("default");
break;
}
}
As can be seen, the compression here works out better.
com.example.dummy.type="Type";var test,test1,test2;var
st=com.example.dummy.type;function execute_case(a){switch(a){case
0:test=st;case 1:test1=st;break;case
2:test2=st;break;default:console.log("default");break}};
No comments:
Post a Comment