Skip to content

Reverse Enum Member Initialization Order for Constants #12693

Closed

Description

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.

Activity

  1. mhegazy commented on Dec 6, 2016

    @mhegazy
    Contributor

    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?

  2. DanielRosenwasser commented on Dec 6, 2016

    @DanielRosenwasser
    Member

    Mohamed Hegazy (@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.

  3. added
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    Won't FixThe severity and priority of this issue do not warrant the time or complexity needed to fix it
    and removed
    SuggestionAn idea for TypeScript
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    on Jan 24, 2017
  4. RyanCavanaugh commented on Jan 24, 2017

    @RyanCavanaugh
    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.

  5. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Won't FixThe severity and priority of this issue do not warrant the time or complexity needed to fix it

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions