Skip to content
Tech News
← Back to articles

Recursion is lying to you

read original more articles
Why This Matters

This article highlights the limitations of recursion in programming, emphasizing that despite its elegance and natural fit for certain problems, recursion can lead to stack overflow errors at large depths due to physical memory constraints. It underscores the importance for developers to understand these operational boundaries and consider alternatives like tail call optimization to prevent silent failures in their code.

Key Takeaways

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 ) ;

Now push a big input:

sum ( 100000 ) ;

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.

... continue reading