Skip to content

Constant fold enum member initializers. #917

Closed
@mbebenita

Description

@mbebenita

Consider the following enum definition:

enum Flags {
    A = 1,
    B = 2,
    C = A | B // Should be constant folded to 3.
}

In most cases, JS engines can optimize simple expressions like these. But they can't do much about accesses to Flags.C.

switch (x) {
  case Flags.A: ... break; // Compiled as case 1: ...
  case Flags.B: ... break; // Compiled as case 2: ...
  case Flags.C: ... break; // Compiled as Flags.C: ...  
}

This prevents JS engines from using a table jump and causes them to fall back on a sequential if/else if implementation for the switch which is all sorts of terrible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueHelp WantedYou can do thisIn DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions