문제

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,

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.

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}};
도움이 되었습니까?

해결책

That optimization makes sense for uncompressed source code, but not if the file is delivered with gzip compression (which is highly recommended).

String aliasing tends to make the compressed source code larger.

See the Closure-compiler FAQ

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top