Why Recursion Exists and What Loops Cannot Express Cleanly in DSA Javascript - Performance Analysis
We want to understand why recursion is useful and how it compares to loops in terms of time cost.
What kind of repeated work does recursion do that loops might not express clearly?
Analyze the time complexity of this recursive function that calculates factorial.
function factorial(n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
This function calls itself to multiply numbers down to 1, calculating factorial.
Look at what repeats in this recursive function.
- Primary operation: One multiplication and one recursive call per function call.
- How many times: Exactly n times, decreasing n by 1 each call until 1.
Each call does a small fixed amount of work and calls itself once with a smaller number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 multiplications and calls |
| 100 | 100 multiplications and calls |
| 1000 | 1000 multiplications and calls |
Pattern observation: The number of operations grows directly with n, so doubling n doubles work.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the input size.
[X] Wrong: "Recursion always makes code slower than loops."
[OK] Correct: Both recursion and loops can do the same amount of work; recursion just expresses some problems more clearly, especially those with repeated branching or nested calls.
Understanding recursion's time cost helps you explain why it's chosen for some problems and shows you can think about how code grows with input size.
"What if the factorial function used a loop instead of recursion? How would the time complexity change?"
Practice
function factorial(n) { if (n === 0) return 1; return n * factorial(n - 1); }Solution
Step 1: Understand recursion's purpose
Recursion solves problems by breaking them into smaller similar problems, like factorial breaking n! into n * (n-1)!.Step 2: Compare recursion and loops
Loops repeat actions but do not naturally express self-similar or nested structures, which recursion handles clearly.Final Answer:
Recursion can express problems with self-similar structure more naturally. -> Option BQuick Check:
Recursion = natural for self-similar problems [OK]
- Thinking loops are always slower
- Believing recursion uses less memory
- Assuming loops cannot repeat
Solution
Step 1: Identify recursive call structure
function recurse(n) { if (n <= 0) return; recurse(n - 1); } calls itself with a smaller argument and has a base case to stop recursion.Step 2: Check other options for recursion correctness
The options using for or while loops include recursive calls but lack base cases, leading to infinite recursion. The option that returns n without a recursive call is not recursive.Final Answer:
function recurse(n) { if (n <= 0) return; recurse(n - 1); } -> Option DQuick Check:
Correct recursion has base case and self-call [OK]
- Missing base case causing infinite recursion
- Using loops instead of recursion
- No recursive call inside function
function sumTo(n) { if (n === 0) return 0; return n + sumTo(n - 1); } console.log(sumTo(3));Solution
Step 1: Trace recursive calls
sumTo(3) calls sumTo(2), sumTo(1), sumTo(0). sumTo(0) returns 0.Step 2: Calculate return values
sumTo(1) = 1 + 0 = 1, sumTo(2) = 2 + 1 = 3, sumTo(3) = 3 + 3 = 6.Final Answer:
6 -> Option AQuick Check:
Sum 1+2+3 = 6 [OK]
- Confusing base case return value
- Adding incorrectly during return
- Expecting loop output instead
function factorial(n) { if (n === 0) return 1; else return n * factorial(n); }Solution
Step 1: Check recursive call argument
The function calls factorial(n) again without reducing n, causing infinite recursion.Step 2: Identify fix
The recursive call should be factorial(n - 1) to move toward base case.Final Answer:
Recursive call uses same argument causing infinite recursion -> Option AQuick Check:
Recursive call must reduce argument [OK]
- Not reducing argument in recursive call
- Ignoring base case
- Confusing recursion with loops
Solution
Step 1: Analyze problem structures
Sum and max in flat arrays are simple linear tasks suited for loops.Step 2: Identify nested self-similar structure
Directory trees have folders inside folders, a nested structure best handled by recursion.Final Answer:
Traversing a directory tree to list all files and folders -> Option CQuick Check:
Nested structures = recursion best [OK]
- Using recursion for simple linear tasks
- Ignoring nested structure complexity
- Confusing repetition with recursion need
Ready to prove it?
Pass the test and this pattern moves unknown
