The whole article – With or without enums in TypeScript
Approach | Enum | Const enum | Const enum + preserveConstEnums |
Object + as const |
Union type |
---|---|---|---|---|---|
Numeric values | 126 | 44 | 112 | 80 | 44 |
Heterogeneous values | 124 | 48 | 117 | 83 | 48 |
String values | 116 | 49 | 108 | 83 | 49 |
When you need to keep lookup object (enum, const enum + preserveConstEnums
and object + as const
), the optimal solution is always an object + as const
.
When you don't need a lookup object (const enum and union type), both const enum and union type are optimal.
-
TypeScript Playground to get JS output
-
size-limit to calculate the size of each JS files
Enums:
Const enums:
Const enums + enabled preserveConstEnums
:
Objects + as const
:
Union types: