Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse Enum Member Initialization Order for Constants #12693

Closed
TylerBrinkley opened this issue Dec 6, 2016 · 3 comments
Closed

Reverse Enum Member Initialization Order for Constants #12693

TylerBrinkley opened this issue Dec 6, 2016 · 3 comments
Labels
Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it

Comments

@TylerBrinkley
Copy link

Consider the following enum.

enum A {
    Name,
    LongName,
    ReallyLongName
}

It's javascript output is

var A;
(function (A) {
    A[A["Name"] = 0] = "Name";
    A[A["LongName"] = 1] = "LongName";
    A[A["ReallyLongName"] = 2] = "ReallyLongName";
})(A || (A = {}));

but would be much more succinct if it were output as

var A;
(function (A) {
    A[A[0] = "Name"] = 0;
    A[A[1] = "LongName"] = 1;
    A[A[2] = "ReallyLongName"] = 2;
})(A || (A = {}));

The only benefit to this change would be minification of the output javascript. Running the two versions in a minifier I'm getting the current output minified at 116 bytes and the proposed way at 96 bytes for a difference of about 17%. Obviously, this difference would be influenced by the number of enum members and the length of the enum members' names.

This change would only be applicable to enum members with a constant value.

@mhegazy
Copy link
Contributor

mhegazy commented Dec 6, 2016

this assumes a side-effect free initialization expressions. consider:

enum A {
    Name = increment(),
    LongName = increment(),
    ReallyLongName = increment()
}

with getters being part of the language, any property access can not guaranteed to be side-effect free, so we would have different emit for enums with auto-initialized members or const-expression-initialized, vs. ones with non-const-expression-initialized enums.

why not use const enums instead?

@DanielRosenwasser DanielRosenwasser added the Suggestion An idea for TypeScript label Dec 6, 2016
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Dec 6, 2016

@mhegazy

why not use const enums instead?

const enums are early-bound which is not always desirable for dependencies whose enums should be late-bound. If you use preserveConstEnums to hack around that for consumers (like we do), you still run into the same exact problem.

this assumes a side-effect free initialization expressions

Precisely - the suggestion here is for enums which are known to be const-initialized and side-effect free, something we already have means of tracking.

@mhegazy mhegazy added the In Discussion Not yet reached consensus label Dec 6, 2016
@RyanCavanaugh RyanCavanaugh added Working as Intended The behavior described is the intended behavior; this is not a bug Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript Working as Intended The behavior described is the intended behavior; this is not a bug labels Jan 24, 2017
@RyanCavanaugh
Copy link
Member

It'd be a runtime breaking change to do this because enums with duplicate values would reverse map to a different name in this emit. const enum remains the best option for size-conscious codebases.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it
Projects
None yet
Development

No branches or pull requests

4 participants