You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
We've talked about this before as something we'd like to do -- the old compiler would do this under an experimental flag and it was nice for a small perf boost. This also would be a necessary first step for a "zero emit" enum that some we had interest in.
As a rough outline I'd expect this to support math over |, &, ~, +, -, integer literals, and previously-declared enum members.
Activity
RyanCavanaugh commentedon Oct 18, 2014
We've talked about this before as something we'd like to do -- the old compiler would do this under an experimental flag and it was nice for a small perf boost. This also would be a necessary first step for a "zero emit" enum that some we had interest in.
As a rough outline I'd expect this to support math over
|,&,~,+,-, integer literals, and previously-declared enum members.DanielRosenwasser commentedon Oct 25, 2014
I think I'll take this on.
vladima commentedon Oct 25, 2014
Oops, I've already started working on this one.
From: Daniel Rosenwassermailto:notifications@github.com
Sent: ý10/ý25/ý2014 11:22 AM
To: Microsoft/TypeScriptmailto:TypeScript@noreply.github.com
Subject: Re: [TypeScript] Constant fold enum member initializers. (#917)
I think I'll take this on.
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/917#issuecomment-60491992.
DanielRosenwasser commentedon Oct 25, 2014
Oops 😄 - I currently propagate arithmetic ops on constants but not previously declared enums. You're probably farther than me, right?
vladima commentedon Nov 4, 2014
covered by #970