Numeric enums in Typescript - Time & Space Complexity
We want to understand how the time it takes to work with numeric enums changes as the number of enum members grows.
Specifically, how does accessing or searching enum values behave when the enum size increases?
Analyze the time complexity of the following code snippet.
enum Colors {
Red, // 0
Green, // 1
Blue // 2
}
function getColorName(value: number): string | undefined {
return Colors[value];
}
This code defines a numeric enum and a function that looks up the name of a color by its numeric value.
- Primary operation: Direct lookup in an object (enum reverse mapping)
- How many times: Single operation per lookup, no loops or recursion
Looking up a value in the enum is like checking a label on a box directly, no matter how many boxes there are.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time to find a name does not grow with the number of enum members; it stays constant.
Time Complexity: O(1)
This means looking up a numeric enum value takes the same amount of time no matter how many items are in the enum.
[X] Wrong: "Looking up a numeric enum value takes longer as the enum gets bigger because it has to check each item."
[OK] Correct: Numeric enums in TypeScript create an object with direct keys, so the lookup is a quick direct access, not a search through all items.
Understanding how enum lookups work helps you explain efficient data access in TypeScript, showing you know how language features impact performance.
"What if we changed the enum to a string enum? How would the time complexity of looking up a name by value change?"
Practice
Solution
Step 1: Understand enum purpose
Numeric enums give meaningful names to numbers, making code easier to read.Step 2: Compare options
Only To assign names to numeric values for better code clarity correctly describes numeric enums as naming numbers for clarity.Final Answer:
To assign names to numeric values for better code clarity -> Option BQuick Check:
Numeric enums = named numbers [OK]
- Confusing enums with string constants
- Thinking enums declare variables
- Assuming enums define functions
Solution
Step 1: Recall enum syntax
TypeScript enums use curly braces with members separated by commas, and assignment uses '='.Step 2: Check each option
enum Colors { Red = 1, Green, Blue } uses correct syntax: enum name, braces, members with '=' and commas.Final Answer:
enum Colors { Red = 1, Green, Blue } -> Option AQuick Check:
Correct enum syntax uses braces and '=' [OK]
- Using '=' after enum name
- Using colons instead of '=' for values
- Separating members with semicolons
enum Status {
Ready = 3,
Waiting,
Done
}
console.log(Status.Waiting);Solution
Step 1: Understand enum auto-increment
Ready is set to 3, so Waiting becomes 4, and Done becomes 5 automatically.Step 2: Identify logged value
console.log(Status.Waiting) prints 4, the auto-incremented value after 3.Final Answer:
4 -> Option CQuick Check:
Auto-increment after 3 = 4 [OK]
- Assuming Waiting equals 3
- Thinking enum values reset automatically
- Expecting string output
enum Direction {
Up = 1,
Down = 2
Left = 3,
Right = 4
}Solution
Step 1: Check enum member separators
Enum members must be separated by commas. After 'Down = 2' there is no comma.Step 2: Validate other rules
Assigned values are allowed, enum names can be uppercase, and mixing assigned/unassigned is allowed.Final Answer:
Missing comma after Down = 2 -> Option DQuick Check:
Enum members need commas [OK]
- Forgetting commas between members
- Thinking enum names must be lowercase
- Believing all members must be unassigned
enum Levels {
Low = 1,
Medium = Low * 2,
High = Medium + 2
}What is the value of
Levels.High?Solution
Step 1: Calculate Medium value
Medium = Low * 2 = 1 * 2 = 2.Step 2: Calculate High value
High = Medium + 2 = 2 + 2 = 4.Step 3: Re-check calculations
Medium is 2, High is 4. This is correct.Step 4: Confirm enum constant expressions
TypeScript allows constant expressions. Low=1, Medium=2, High=4.Final Answer:
4 -> Option AQuick Check:
Levels.High = 4 [OK]
- Ignoring expression evaluation order
- Assuming High equals Medium + Low
- Confusing enum values with indexes
Ready to prove it?
Pass the test and this pattern moves unknown
