Your Recursion Is Lying to You
Summary:
The latest in Gabor Koos's 'Lying to You' series on elegant abstractions with hidden operational edges. The lie here: a recursive function can be logically perfect and still crash, because every call consumes stack, and the textbook rescue, tail-call optimization, is a spec fiction in practice. ECMAScript 2015 formally specified proper tail calls, but V8 and SpiderMonkey never shipped them and JavaScriptCore shipped and walked them back, so correctly tail-recursive code still overflows in the runtimes your users actually run. Two sharp distinctions make the piece: tail recursion is a property of code structure while stack reuse is a property of the engine, and Fibonacci is a red herring because its exponential blowup fails identically from the outside but for an entirely different reason. The production advice is unglamorous and right: rewrite deep recursion as iteration or an explicit stack.
Excerpt:
"A logically correct recursive function is still a stack overflow waiting on a big enough input, and tail-call optimization will not save you: JavaScript engines never really shipped it."
#JavaScript#Software Engineering#Reliability
Read Full Source