Question

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}};
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top