> Each call branches into two more calls, so the total number of calls grows as O(2ⁿ).
Well, no, not really. If anyone bothers counting how many recursive calls are actually made, the result is far from powers of two:
n | result | # of calls
1 | 1 | 1
2 | 1 | 3
3 | 2 | 5
4 | 3 | 9
5 | 5 | 15
6 | 8 | 25
7 | 13 | 41
8 | 21 | 67
9 | 34 | 109
10 | 55 | 177
11 | 89 | 287
12 | 144 | 465
13 | 233 | 753
14 | 377 | 1219
15 | 610 | 1973
16 | 987 | 3193
17 | 1597 | 5167
18 | 2584 | 8361
19 | 4181 | 13529
20 | 6765 | 21891
A curious person will then calculate the actual ratio: n | result | # of calls | ratio
1 | 1 | 1 | 1
2 | 1 | 3 | 3
3 | 2 | 5 | 1.6666666666666667
4 | 3 | 9 | 1.8
5 | 5 | 15 | 1.6666666666666667
6 | 8 | 25 | 1.6666666666666667
7 | 13 | 41 | 1.64
8 | 21 | 67 | 1.6341463414634145
9 | 34 | 109 | 1.626865671641791
10 | 55 | 177 | 1.6238532110091743
11 | 89 | 287 | 1.6214689265536724
12 | 144 | 465 | 1.6202090592334495
13 | 233 | 753 | 1.6193548387096774
14 | 377 | 1219 | 1.6188579017264275
15 | 610 | 1973 | 1.6185397867104183
16 | 987 | 3193 | 1.6183476938672072
17 | 1597 | 5167 | 1.6182273723770748
18 | 2584 | 8361 | 1.6181536675053223
19 | 4181 | 13529 | 1.6181078818323167
20 | 6765 | 21891 | 1.6180796806859339
and will notice that it gets close to φ = (1 + √5) / 2 ≈ 1.618033989, which makes the number of recursive calls O(φⁿ), which is much more fun than O(2ⁿ).more to the point, i feel like there should be a compiler switch or decorator style flag in modern languages that declare "this function is expected to optimize with tail recursion, throw a compiler or linter error at static analysis time if that doesn't work out."
Proper Tail Calls were implemented behind experimental flags but never shipped by default — the flags were later removed.
In the meantime, the theory of structured recursion[recursion schemes] has been developing, yet no language offers then as first class constructs. The best we get is library support. Imagine if we had to import a package to support if statements. The result? Programmers write recursive programs while navigating all the foot guns described in the article. No wonder recursion is hard to get right.
Scala and Kotlin have that. Other modern languages probably do as well.
Using a jmp isn’t really a call as you’d know, and information is lost that would be crucial to unwinding a recursion.
A recurse keyword would still leave the person writing the code with the decision with proving termination with base cases or trampolines — lest there is just a bunch of math that would unwind your recursion into a better bounded problem. Which is kind of what current keywords do anyway.
If that's what you mean then I'm afraid it sounds like you're mixing a few things up. For example, imagine depth-first search: you're going to need a stack somewhere, whether it's the CPU stack which you use via recursion, or an explicit stack you use via iteration. Iterating doesn't magically remove your need for that space and somehow collapse everything down to one stack frame. And you can reuse temporaries from the heap too, etc.
Fundamentally, there is the question of how much space you need for given algorithm, the question of what algorithm you should use in the first place, the question of whether that particular algorithm should be implemented recursively or iteratively, and the question of what is more maintainable and easier to evolve in practice.
These are all separate questions, but you're conflating them. If your iteration uses constant space but your recursion doesn't, that's because you're not implementing the same algorithm. You're implementing a different algorithm that achieves the same original goal you had. Of course one algorithm might beat the other, that's no surprise.
For instance:
"All current mainstream [C++] compilers perform tail call optimisation fairly well (and have done for more than a decade), even for mutually recursive calls" [1].
"As of July 22, 2023 Safari is the only browser that supports tail call optimization" of JavaScript [2].
"Since Clojure uses the Java calling conventions, it cannot, and does not, make the same tail call optimization guarantees. Instead, it provides the recur special operator, which does constant-space recursive looping" [3].
"The Scala compiler will automatically optimize any truly tail-recursive method. If you annotate a method that you believe is tail-recursive with the @tailrec annotation, then the compiler will warn you if the method is actually not tail-recursive" [4].
[1]: https://stackoverflow.com/a/34129
[2]: https://stackoverflow.com/a/37224563
it IS possible for a loop to use constant space. pre-allocate temporaries and inline any function calls. everything lives in one stack frame. simply iterating the loop itself does not come with fixed space overheads from allocating new stack frames.
i suppose one thing i should mention, i am thinking of extreme optimization use cases where the entire data structure fits in cache (or close to it). think like in-cache tries or similar. if you're hitting main memory with each iteration anyway, it doesn't really matter.
Featured in Node Weekly #624 and Javascript Weekly - 2026-06-02*
Recursion is one of those ideas developers learn early and trust for years. If the recursive step is simple and the base case is correct, the code feels clean and safe.
It is elegant for a reason: many problems are naturally recursive, and the code often mirrors how we explain the logic out loud. For tree walks, nested structures, and divide-and-conquer patterns, recursion can be easier to read than explicit loops.
The catch is physical limits. Even with a correct base case and sound logic, each recursive call still consumes stack space. At some depth, you crash with stack overflow.
If you read Your Debounce Is Lying to You and Your Throttling Is Lying to You, this is the recursion version of the same pattern: elegant abstraction, hidden operational edge. Even dependency management can lie to you, as explored in Your Package Manager Is Lying to You. For a different category of silent failure, Your JS Date Is Lying to You covers the parsing, mutation, and timezone traps built into the JavaScript Date API.
You can run everything below directly in a browser console. Let's start simple: a recursive sum of all integers from 1 to n.
function sum(n) {
if (n === 0) return 0;
return n + sum(n - 1);
}
sum(10); // 55
Now push a big input:
sum(100000); // RangeError or InternalError: too much recursion in most JS runtimes
What just happened? The function is logically correct, but each call to sum stays on the stack until the one below it returns. At depth 100,000 the runtime runs out of stack space and throws. It has nothing to do with the result being wrong, it is purely a physical limit on how many nested frames the runtime can hold at once.
The usual next step is tail call optimization. The idea is simple: make the recursive call the last thing the function does, so the runtime can reuse the same frame instead of pushing a new one.
Note that sum is not tail-recursive, even though the recursive call appears on the last line. After sum(n - 1) returns, there is still pending work: the result must be added to n. A call is only in tail position when its return value is forwarded immediately, with no pending computation afterward.
The tail-recursive version moves that pending state into an accumulator:
function sumTR(n, acc = 0) {
if (n === 0) return acc;
return sumTR(n - 1, acc + n);
}
sumTR(10); // 55
Here sumTR(...) is the very last thing that happens — no pending +, no pending anything. The running total lives in acc, not in waiting stack frames. In theory, a runtime that implements TCO can execute this in constant stack space regardless of depth.
Now repeat the same stress input:
sumTR(100000); // may still throw RangeError!
Even with correct tail-recursive structure, many JavaScript runtimes still allocate a new stack frame per call and throw at large depth. This surprises developers who expect TCO to be a universal guarantee. ECMAScript 2015 formally specified proper tail calls in strict mode, but most engines never adopted the feature consistently. Some shipped it and then walked it back due to performance regressions. Others never implemented it at all. The result is that you cannot assume tail recursion is stack-safe in production JavaScript, even if the code is correctly structured for TCO.
Fibonacci is the go-to recursion textbook example and it does run into stack limits too, but it carries a second problem that makes it even worse: exponential time complexity.
function fib(n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
Each call branches into two more calls, so the total number of calls grows as O(2ⁿ). fib(30) already makes over a million calls; fib(50) is in the tens of billions. In a browser this freezes the tab long before any stack limit is reached, which makes the failure mode look identical to a stack overflow but have a completely different root cause.
The tail-recursive version of Fibonacci:
function fibTR(n, a = 0, b = 1) {
if (n === 0) return a;
if (n === 1) return b;
return fibTR(n - 1, b, a + b);
}
This version runs in linear time, but it still risks stack overflow at large n due to the same TCO uncertainty. The exponential version is a red herring for this discussion because it fails for a completely different reason: stack overflow and exponential blowup are two separate problems. They look the same from the outside (the page hangs or crashes) but require completely different fixes.
At the time of writing (May 2026), proper tail-call optimization support is not something you can count on across JavaScript runtimes.
| Runtime | Engine | Proper Tail Calls You Can Rely On? | Practical Take |
|---|---|---|---|
| Chrome | V8 | No | Do not expect stack-safe tail recursion. |
| Node.js | V8 | No | Tail-recursive code can still overflow. |
| Deno | V8 | No | Same operational expectation as Node/Chrome. |
| Firefox | SpiderMonkey | No | Do not treat tail recursion as a safety guarantee. |
| Safari | JavaScriptCore | Inconsistent — JSC has shipped and walked back TCO across versions | Do not rely on it; behavior has varied enough across releases that it is not a stable guarantee. |
| Bun | JavaScriptCore-based | Engine-dependent, not a cross-runtime guarantee | Verify on exact version; do not assume universal behavior. |
The key point is portability. Tail recursion is a property of function structure, while stack reuse is a property of runtime implementation. Even if one engine behaves better in one version, production JavaScript usually spans multiple targets, and correctness should not depend on optimizer-specific behavior. A function can be perfectly tail-recursive in shape and still consume stack per call in the environments your users actually run.
Every recursive function can be rewritten iteratively, and that is usually the safest choice in production when input depth can grow. Iteration does not rely on runtime optimizations for stack safety, because it does not consume stack frames per step. This does not mean giving up the recursive mental model. You can still write code that is conceptually recursive but uses an explicit stack or a trampoline to manage control flow without hitting physical limits.
function sumIter(n) {
let acc = 0;
for (let i = n; i > 0; i--) acc += i;
return acc;
}
sumIter(1000000); // no recursive stack growth
If you want to keep the recursive structure for readability but need to avoid stack growth, you can use a trampoline: a loop that repeatedly calls a function that returns either a final result or another function to call.
function trampoline(fn) {
let result = fn;
while (typeof result === 'function') {
result = result();
}
return result;
}
function sumTrampoline(n, acc = 0) {
if (n === 0) return acc;
return () => sumTrampoline(n - 1, acc + n);
}
trampoline(() => sumTrampoline(100000)); // no stack overflow, still tail-recursive in spirit
Trampolines trade stack safety for additional function allocations and dispatch overhead, so they are most useful when preserving recursive structure matters more than raw performance.
This approach scales in a way that does not depend on runtime tail-call behavior, which is exactly what you want when input depth can grow. If recursive structure improves readability for a particular problem, these techniques let you keep that mental model with explicit tradeoffs instead of implicit runtime assumptions.
A useful rule of thumb is to keep recursion for small, bounded depths that you control, and switch to iterative control flow as soon as depth is user-driven, data-driven, or operationally uncertain. For hot paths, benchmark both styles, but do not base correctness on assumed TCO.
Recursion itself is not the enemy, unverified runtime assumptions are. Tail-recursive shape does not automatically make JavaScript stack-safe, and that gap is where many "works on my machine" surprises come from in production.
Use recursion where it improves clarity and depth is genuinely bounded. When depth can grow or input is outside your control, prefer iterative designs that make stack behavior explicit and portable.