closure-compiler
closure-compiler copied to clipboard
Alias for long exported names
One of my takeaways from reading the documentation on externs vs exports was that externs are discouraged as a way to prevent mangling of your own api/data model.
It made sense to me that exports via obj['somePropertyName']
might be more optimized since the compiler can use internal aliases rather than carrying forward the full name everywhere.
I decided to test this out with the example below:
function createPersonModel(data) {
var fullName = data['info']['firstNameProperty'] + data['info']['lastNameProperty'];
var fullNameReverse = data['info']['lastNameProperty'] + data['info']['firstNameProperty'];
console.log(fullName);
console.log(fullNameReverse);
}
createPersonModel(anObjectFromAnApi);
Let's just assume this is a deep object returned from an api, and I am using ['']
notation to prevent mangling.
Based on what I read in the documentation I was expecting the repeated property references to be aliased?
Instead the code compiled to the following:
var a=anObjectFromAnApi.info.lastNameProperty+anObjectFromAnApi.info.firstNameProperty;
console.log(anObjectFromAnApi.info.firstNameProperty+anObjectFromAnApi.info.lastNameProperty);
console.log(a);
Isn't there an opportunity here to assign anObjectFromAnApi.info.lastNameProperty
and anObjectFromAnApi.info.lastNameProperty
to single character aliases?
I also tried exaggerating this by adding several more references to these properties to see if an alias would be introduced. The behavior was the same though.
Am I misunderstanding the point about aliasing of exports when compared to externs?
Potentially, as the compiler generally assumes properties reads are without visible side-effects. But, of course, in the general case that isn't true as any of the property read could be getters with sideeffects.
Currently, the compiler doesn't have a general subexpression elimination pass, which would be what would be required here.
Any updates on this? For now, if the compiled code uses a repetitive, verbose external library, even if it's all properly annotated in externs, the output is much larger than what it could be after aliasing.