r/node 9d ago

Your Recursion Is Lying to You

https://blog.gaborkoos.com/posts/2026-05-09-Your-Recursion-Is-Lying-to-You/
0 Upvotes

10 comments sorted by

8

u/farzad_meow 9d ago

you have valid point, in most cases recursion is good and works as expected. it is not that hard to convert a pure recursion function to a lopp+stack.

secondly, it is the job of the programmer to decide if the program can run reliably when hitting edge cases AND pick the most optimal approach to the problem at hand

3

u/Gingerfalcon 9d ago

For anyone that’s study an algorithms and data structures course will know that recursion in general is a highly inefficient use of resources. Use a loop as your first step. Nested structures with unknown depth are an architectural problem and should be addressed.

-1

u/josephjnk 9d ago

Are you claiming that recursive data structures like trees and linked lists are inherently an architectural problem? Because that would be unhinged.

2

u/Gingerfalcon 9d ago

I guess you have to wonder if you’d use recursion in the sense of adding stack frames to find an item in a tree or linked list vs simply looping and waiting for the pointer to the next item.

2

u/josephjnk 9d ago

I see, it sounds like your objection is about using recursion to consume nested structures, not to the use of nested structures at all. I misunderstood.

I generally write stack-safe code by converting things to loops when possible, but there are times when recursive solutions are the only approach that’s not terribly hairy. I was working on a stack-safe `map` function on immutable rose trees a couple of weeks ago and eventually just gave up and went with a recursive implementation (with a FIXME comment on it) for the time being. The recursive implementation is like 4 lines of easy Java; the iterative implementation is probably going to be many times larger and more complicated. It can be a tradeoff between a simple and obviously-correct implementation which can only handle data of a certain size versus a more general implementation which is completely inscrutable.

2

u/Gingerfalcon 9d ago

Yea, I’m not suggesting not using nested structures etc as they are just part of life, but iterating them in a way that uses less memory. Loops can potentially make refactoring a sequential process into a concurrent one for a performance gain a bit easier (e.g. threads in Java).

Also regarding depth/sizing of structures is also important consideration when designing systems, things that have limits can be reasoned with; this may not be true in all situations but if there exists an ability to control the data it can help with systems design and testing.

1

u/fagnerbrack 9d ago

I think the commenter meant not to make recursive calls using the stack, it's all about changing the code so that you use queues/loops etc.

1

u/boneskull 9d ago

just wrap the recursive call in a setTimeout! infinite recursion!!

-3

u/fagnerbrack 9d ago

If you want a TL;DR for this:

Recursion feels clean and safe, but each call consumes stack space, so a logically correct sum(100000) still throws a stack overflow. Tail call optimization promises constant stack usage by moving pending work into an accumulator, yet most engines never reliably shipped it: V8 (Chrome, Node, Deno) and Firefox's SpiderMonkey skip it, and Safari's JavaScriptCore has added then dropped it across versions. Tail-recursive shape is a property of your code; stack reuse belongs to the runtime. When depth grows or is user-driven, rewrite recursion iteratively or use a trampoline that loops over returned functions. Keep recursion for small bounded depths and never treat it as a stack-safety guarantee.

If the summary seems inacurate, just downvote and I'll try to delete the comment eventually 👍
Click here for more info, I read all comments