Why enums are needed in Typescript - Performance Analysis
We want to understand how using enums affects the time it takes for a program to run.
Specifically, we ask: does choosing enums change how fast the code works as input grows?
Analyze the time complexity of the following code snippet.
enum Direction {
Up,
Down,
Left,
Right
}
function move(direction: Direction) {
switch(direction) {
case Direction.Up: return 'Moving up';
case Direction.Down: return 'Moving down';
case Direction.Left: return 'Moving left';
case Direction.Right: return 'Moving right';
}
}
This code uses an enum to represent directions and a function to return a message based on the direction.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The switch statement checks the input once per function call.
- How many times: Exactly once each time the function runs.
The function runs the switch once per call, no matter how many directions exist or how many times it is called.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks (one per call) |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with how many times the function is called, not with the number of enum items.
Time Complexity: O(n)
This means the time grows linearly with the number of function calls, not with the enum size.
[X] Wrong: "Using enums makes the program slower because it adds extra checks."
[OK] Correct: Enums are just named values. The checks happen anyway, with or without enums, so enums do not add extra time complexity.
Understanding how enums affect code speed helps you write clear and efficient programs, a skill valued in many coding tasks.
"What if we replaced the switch with a lookup object? How would the time complexity change?"
Practice
enum Color { Red, Green, Blue }Solution
Step 1: Understand what enums represent
Enums group a fixed set of related values under friendly names, improving code clarity.Step 2: Analyze the options
Only They group related values with readable names. correctly describes enums' purpose. Options A, B, and C are incorrect features.Final Answer:
They group related values with readable names. -> Option AQuick Check:
Enums improve readability = D [OK]
- Thinking enums allow adding values at runtime
- Confusing enums with functions
- Believing enums convert numbers to strings automatically
Solution
Step 1: Recall enum declaration syntax
In TypeScript, enums are declared using the keywordenumfollowed by curly braces with comma-separated names.Step 2: Check each option
enum Direction { Up, Down, Left, Right } matches the correct syntax. Options A and C use invalid assignment or colon syntax. enum Direction { 'Up', 'Down', 'Left', 'Right' } incorrectly uses quotes around names.Final Answer:
enum Direction { Up, Down, Left, Right } -> Option AQuick Check:
Correct enum syntax uses braces and names without quotes = B [OK]
- Using equals sign or colon instead of braces
- Putting quotes around enum member names
- Ending enum declaration with semicolon incorrectly
enum Status { Active = 1, Inactive, Pending }
console.log(Status.Inactive);Solution
Step 1: Understand enum numeric values
The enum starts with Active = 1, so Inactive gets the next number, 2, and Pending gets 3.Step 2: Check the logged value
LoggingStatus.Inactiveprints its numeric value, which is 2.Final Answer:
2 -> Option DQuick Check:
Enum auto-increments numbers = A [OK]
- Assuming enum starts at 0 when first value is assigned 1
- Expecting string output instead of number
- Confusing enum member names with their values
enum Size {
Small = 'S',
Medium = 'M',
Large = 3
}Solution
Step 1: Check enum value types
TypeScript enums can be numeric or string, but mixing both types in one enum causes errors.Step 2: Analyze the given enum
Here, 'Small' and 'Medium' have string values, but 'Large' has a number, which is invalid.Final Answer:
Mixing string and number values in one enum is not allowed. -> Option BQuick Check:
Enum values must be consistent type = C [OK]
- Thinking enum member names must be lowercase
- Believing enum values must be unique numbers only
- Assuming missing semicolon causes error
enum UserRole { Admin, Editor, Viewer }Solution
Step 1: Understand the benefit of enums for fixed sets
Enums provide named constants that prevent typos and improve readability and maintainability.Step 2: Evaluate the options
Enums prevent typos and make code easier to maintain. correctly states the main advantage. Options B, C, and D describe incorrect or unrelated features.Final Answer:
Enums prevent typos and make code easier to maintain. -> Option CQuick Check:
Enums improve safety and clarity = A [OK]
- Believing enums add dynamic behavior
- Thinking enums provide encryption
- Assuming enums optimize database storage automatically
Ready to prove it?
Pass the test and this pattern moves unknown
