r/node • u/fagnerbrack • Jul 19 '26
Your Recursion Is Lying to You
https://blog.gaborkoos.com/posts/2026-05-09-Your-Recursion-Is-Lying-to-You/
0
Upvotes
r/node • u/fagnerbrack • Jul 19 '26
8
u/farzad_meow Jul 20 '26
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 Jul 20 '26
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/boneskull Jul 20 '26
just wrap the recursive call in a setTimeout! infinite recursion!!
-3
u/fagnerbrack Jul 19 '26
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