Algorithms and Data Structures · Middle · Question 51 of 75
How can you avoid a stack overflow in deep recursion in JavaScript?
- 1Mark the recursive function as
async - 2Wrap the recursive call in
try...catch - ✓Correct answer: Rewrite it with a loop and an explicit stack
Explanation
JavaScript engines (except Safari) don't implement tail call optimization, so the call stack is limited to roughly ten thousand frames. An explicit array used as a stack lives on the heap and can grow much larger.