Maximum call stack size exceeded — why it happens and how to fix it, even without recursion

The app was working. You loaded a big data file — and the console turns red: RangeError: Maximum call stack size exceeded. You google it, and every answer says the same thing: "you have infinite recursion". You look at your code — there's no recursion anywhere. Just one line: Math.max(...prices).
Here's the thing: this error doesn't mean "recursion". It means "the pile of calls overflowed". Recursion is the most common way to overflow it, but not the only one. Let's go through three real causes — from the common one to the surprising one.
What the error means
Every function call goes onto the call stack — a pile holding every function that has started and not yet finished. The pile has a ceiling. In Node.js 22, a simple function can call itself about 12 thousand times before it crashes. We checked.
When the pile hits the ceiling, the engine stops the program with this error. In Python the same thing is called RecursionError, and the ceiling there is about a thousand calls.
Your main search tool is the stack trace under the error. Look at it before you change anything.
Cause 1: a function calls itself forever
The most common one. A function calls itself, which calls itself again — until there's no room left.
How to check. In the stack trace, the same line with the same function name repeats dozens of times in a row. That's practically a signature.
Sometimes it's explicit recursion that's missing its exit condition. And sometimes it's accidental — AI writes this kind of thing a lot. For example, a wrapper with the same name as the original:
async function fetch(url) {
console.log("request:", url);
return fetch(url); // calls itself, not the built-in fetch
}
Your own fetch function shadowed the built-in one. Inside it, the name fetch now points to itself. Every call spawns the next.
How to fix. For explicit recursion — make sure it has a "floor": a condition where it returns a result without calling itself. And that every step gets closer to that floor. For the accidental kind — rename the wrapper: loggedFetch. Then fetch inside it is the built-in one again.
Cause 2: a setter or getter refers to itself
Tricky, because the code looks harmless:
class User {
set name(value) {
this.name = value.trim(); // triggers this same setter again
}
}
A setter is a function that runs when you write to a field. Inside, it writes to this.name — which calls itself. And again. We checked: it crashes with exactly this error. A getter that reads itself does the same.
How to check. The stack trace repeats a line with set name or get name.
How to fix. Store the value in a field with a different name. The modern way is a private field with a hash:
class User {
#name = "";
set name(value) { this.#name = value.trim(); }
get name() { return this.#name; }
}
Cause 3: a huge array spread into arguments
The most surprising one. No recursion at all:
const biggest = Math.max(...prices);
items.push(...newItems);
The three dots spread an array into separate function arguments. And arguments go onto the stack during the call too. With a hundred elements, all is well. With hundreds of thousands, you run out of room. In Node.js 22, Math.max(...) handled about 125 thousand elements for us and crashed on more. The exact limit depends on the browser — which is why it works on your test data and crashes on real data.
How to check. The stack trace has no repeats — just one or two lines, and the last one points to the line with three dots.
How to fix. For max and min — a plain pass over the array:
const biggest = prices.reduce((max, p) => (p > max ? p : max), -Infinity);
For joining arrays — items = items.concat(newItems), or a loop that pushes one at a time.
How to find it fast
A sequence that works almost every time:
- Open the stack trace and find the first line from your own file. How to read messages like this is covered in how to read an error message.
- Check whether it repeats. It does — cause 1 or 2. It doesn't — look nearby for three dots, cause 3.
- Don't raise the stack limit with flags. That hides the bug instead of fixing it.
If you'd rather not dig — give AI the full stack trace and the line it points to. The step-by-step approach is in how to debug code with AI. "Maximum call stack crash, here's the trace, line 42 repeats" gives the model everything it needs.
Is this the same as "Maximum update depth exceeded" in React?
No, but they're related. That one is React protecting itself from an endless update loop: a component changes state on every render. The stack doesn't overflow — React stops the loop itself.
Why does it work for me but crash for my users?
The stack ceiling differs between browsers and depends on how much room each call takes. Code near the limit can work in Chrome and crash in Safari. Fix the cause instead of counting on headroom.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





