Const enums and optimization in Typescript - Time & Space Complexity
We want to see how using const enums affects the speed of our TypeScript code.
How does the program's work change when const enums are used?
Analyze the time complexity of the following code snippet.
const enum Colors {
Red,
Green,
Blue
}
function getColorName(color: Colors): string {
switch(color) {
case Colors.Red: return "Red";
case Colors.Green: return "Green";
case Colors.Blue: return "Blue";
}
// Added default case to avoid missing return
return "Unknown";
}
const colorName = getColorName(Colors.Green);
This code uses a const enum to represent colors and a function to get the color name.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The switch statement runs once per function call.
- How many times: Once for each call to
getColorName.
Since the enum values are replaced at compile time, the function runs with simple values.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 simple switch checks |
| 100 | 100 simple switch checks |
| 1000 | 1000 simple switch checks |
Pattern observation: The work grows linearly with the number of calls, but each call is very fast due to compile-time replacement.
Time Complexity: O(n)
This means the time grows directly with how many times the function is called, but each call is very efficient.
[X] Wrong: "Using const enums makes the code run in constant time no matter how many calls there are."
[OK] Correct: Each function call still runs the switch, so time grows with calls, but const enums make each call faster by replacing values early.
Understanding how compile-time features like const enums affect runtime helps you write faster code and explain your choices clearly.
"What if we replaced the const enum with a regular enum? How would the time complexity and runtime behavior change?"
Practice
const enum in TypeScript?Solution
Step 1: Understand what
const enumdoesconst enumreplaces enum references with their actual values during compilation, so no enum object exists at runtime.Step 2: Compare runtime behavior
Unlike regular enums,const enumdoes not generate extra JavaScript code for the enum object, reducing code size and improving speed.Final Answer:
It replaces enum references with actual values at compile time, reducing runtime code. -> Option CQuick Check:
Const enums optimize code by replacing references with values [OK]
- Thinking const enums create runtime objects
- Believing const enums allow runtime modification
- Confusing const enums with regular enums
Solution
Step 1: Recall the correct keyword order
The correct syntax uses the keywordconstbeforeenumto declare a const enum.Step 2: Check each option
const enum Colors { Red, Green, Blue } matches the correct syntax:const enum Colors { Red, Green, Blue }. Options C and D use invalid keywords or order.Final Answer:
const enum Colors { Red, Green, Blue } -> Option BQuick Check:
Const enum syntax = const enum [OK]
- Writing 'constant' instead of 'const'
- Placing 'const' after 'enum'
- Omitting 'const' keyword
const enum Direction {
Up = 1,
Down,
Left,
Right
}
const move = Direction.Left;
console.log(move);Solution
Step 1: Understand enum value assignment
The enum starts withUp = 1, soDown = 2,Left = 3, andRight = 4.Step 2: Identify the value of
It is 3, soDirection.Leftmoveis assigned 3, andconsole.log(move)prints 3.Final Answer:
3 -> Option AQuick Check:
Enum Left value = 3 [OK]
- Assuming Left is 2 instead of 3
- Expecting string output 'Left'
- Confusing enum with object property
const enum Status {
Active = 1,
Inactive = 0
}
console.log(Status[1]);Solution
Step 1: Understand const enum limitations
Const enums do not generate an object at runtime, so reverse mapping (accessing by index) is not supported.Step 2: Identify the error cause
UsingStatus[1]tries to access reverse mapping, causing a compile-time error.Final Answer:
Compile-time error: Cannot use reverse mapping with const enums -> Option AQuick Check:
Reverse mapping disallowed in const enums [OK]
- Expecting reverse mapping to work with const enums
- Thinking it outputs string 'Active'
- Ignoring compile-time errors
Solution
Step 1: Recall const enum behavior
Const enums inline values and do not create runtime objects, so reverse mapping (index to name) is not available.Step 2: Analyze each option
Options A, B, and D work fine as they use the inlined values directly. Accessing enum values by their numeric index (reverse mapping). requires reverse mapping, which fails with const enums.Final Answer:
Accessing enum values by their numeric index (reverse mapping). -> Option DQuick Check:
Reverse mapping breaks with const enums [OK]
- Assuming all enum features work with const enums
- Using reverse mapping without error
- Confusing const enum with regular enum behavior
Ready to prove it?
Pass the test and this pattern moves unknown
