debugModerate
Maximum call stack size exceeded — detecting and fixing infinite recursion
Viewed 0 times
stack overflowinfinite recursioncircular referencebase casetrampolinestructuredClone
browsernodejs
Error Messages
Problem
JavaScript throws RangeError: Maximum call stack size exceeded. Usually from accidental infinite recursion, circular references in JSON.stringify, or deeply nested data structures.
Solution
Common causes: (1) Missing base case in recursive function. (2) Circular references — use JSON.stringify with replacer or structuredClone. (3) State update triggering re-render that triggers state update (React infinite loop). (4) Getter/setter calling itself. (5) Mutual recursion (A calls B, B calls A). Fix: add base case, use iterative approach for deep structures, detect cycles with a Set of visited objects. For deep data: increase stack with --stack-size=N or use trampolining.
Why
Each function call adds a frame to the call stack. JavaScript engines limit stack depth (typically 10,000-25,000 frames). Infinite recursion exhausts this limit.
Revisions (0)
No revisions yet.