Switch statement in Javascript - Time & Space Complexity
We want to see how the time it takes to run a switch statement changes as the number of cases grows.
How does the program decide which case to run when there are many options?
Analyze the time complexity of the following code snippet.
function getDayName(dayNumber) {
switch(dayNumber) {
case 1: return 'Monday';
case 2: return 'Tuesday';
case 3: return 'Wednesday';
case 4: return 'Thursday';
case 5: return 'Friday';
case 6: return 'Saturday';
case 7: return 'Sunday';
default: return 'Invalid day';
}
}
This code returns the name of the day based on a number using a switch statement.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Index computation and jump table lookup (or equivalent optimization).
- How many times: Constant time (O(1)), independent of number of cases.
As the number of cases grows, the execution time remains nearly constant thanks to JS engine optimizations like jump tables.
| Input Size (number of cases) | Approx. Checks |
|---|---|
| 10 | ~1 check |
| 100 | ~1 check |
| 1000 | ~1 check |
Pattern observation: The number of checks remains constant regardless of the number of cases.
Time Complexity: O(1)
This means the time to find the matching case is constant even as the number of cases increases.
[X] Wrong: "Switch statements check each case one by one like if-else chains, so O(n)."
[OK] Correct: JS engines optimize switch with jump tables, binary search, or hashes for typically O(1) performance.
Understanding how switch statements scale helps you explain your code choices clearly and shows you think about efficiency in real situations.
"What if we replaced the switch statement with an object lookup? How would the time complexity change?"
Practice
switch statement in JavaScript?Solution
Step 1: Understand the role of switch
A switch statement checks a value and runs code matching that value.Step 2: Compare with other options
Repeating code is for loops, defining functions is with function keyword, and variables use let/const/var.Final Answer:
To choose which block of code to run based on a value -> Option AQuick Check:
Switch = choose code by value [OK]
- Confusing switch with loops
- Thinking switch defines functions
- Believing switch creates variables
Solution
Step 1: Recall switch syntax
The switch statement requires parentheses around the value and curly braces for cases: switch (value) { ... }Step 2: Check each option
switch (value) { } matches correct syntax; others miss parentheses or use wrong symbols.Final Answer:
switch (value) { } -> Option CQuick Check:
Correct switch syntax uses parentheses and braces [OK]
- Omitting parentheses around value
- Using curly braces instead of parentheses
- Adding colon after switch keyword
const day = 3;
switch(day) {
case 1:
console.log('Monday');
break;
case 2:
console.log('Tuesday');
break;
case 3:
console.log('Wednesday');
break;
default:
console.log('Unknown day');
}Solution
Step 1: Match the value with cases
The variable day is 3, so the switch looks for case 3.Step 2: Identify executed case
Case 3 matches and prints 'Wednesday', then break stops further checks.Final Answer:
Wednesday -> Option DQuick Check:
day=3 matches case 3 = Wednesday [OK]
- Forgetting break causes fall-through
- Choosing default when case matches
- Mixing up case numbers and outputs
const color = 'red';
switch(color) {
case 'red'
console.log('Stop');
break;
case 'green':
console.log('Go');
break;
default:
console.log('Wait');
}Solution
Step 1: Check case syntax
Each case must end with a colon ':' after the value. Here, case 'red' misses the colon.Step 2: Verify other parts
Break is present after case 'red' block, variable name is correct, and default exists.Final Answer:
Missing colon ':' after case 'red' -> Option BQuick Check:
Case needs ':' after value [OK]
- Omitting colon after case value
- Forgetting break causing fall-through
- Missing default case (not always error)
Solution
Step 1: Group cases for weekend and weekday
Cases 6 and 7 should print 'Weekend' with one console.log and break after.Step 2: Group cases 1 to 5 for 'Weekday' with break
Cases 1-5 print 'Weekday' and break to avoid fall-through.Step 3: Check default for invalid days
Default prints 'Invalid day' if no case matches.Final Answer:
switch(day) { case 6: case 7: console.log('Weekend'); break; case 1: case 2: case 3: case 4: case 5: console.log('Weekday'); break; default: console.log('Invalid day'); } -> Option AQuick Check:
Group cases, use break to stop fall-through [OK]
- Missing break causing fall-through
- Repeating console.log in each case unnecessarily
- Omitting default case for invalid input
Ready to prove it?
Pass the test and this pattern moves unknown
