closure-compiler icon indicating copy to clipboard operation
closure-compiler copied to clipboard

Alias for long exported names

Open thelgevold opened this issue 8 years ago • 2 comments

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?

thelgevold avatar Feb 07 '17 03:02 thelgevold

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.

concavelenz avatar Feb 07 '17 22:02 concavelenz

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.

AdobeScripter avatar Oct 26 '20 21:10 AdobeScripter