Skip to main content

Browser · 6 common causes

Maximum call stack size exceeded

Updated

The call stack grew past the engine's limit — nearly always unbounded recursion.

The exact message

Uncaught RangeError: Maximum call stack size exceeded
    at flatten (utils.js:12:20)
    at flatten (utils.js:15:14)
    at flatten (utils.js:15:14)
    at flatten (utils.js:15:14)
    at flatten (utils.js:15:14)

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is Maximum call stack size exceeded.

How to fix it

Cause 1 of 6

Recursion with no base case, or one that is never reached.

Check that the terminating condition actually fires — log the depth and the argument on entry. A counter decremented past its stopping value, so the check never matches exactly, is the classic version of this.

Cause 2 of 6

A getter or setter that refers to itself.

set value(v) { this.value = v } calls itself forever. Store behind a different name — a backing field such as this._value, or a private #value — so the accessor and the storage are distinct.

Cause 3 of 6

In React, calling a state setter during render.

Setting state in the component body triggers a re-render, which sets it again. Move the call into an event handler, or into an effect with a dependency list that stops it looping.

Cause 4 of 6

Walking a data structure that contains a cycle.

A depth-first traversal of a graph with a back-reference never terminates. Track visited nodes in a Set and skip them. (Note that JSON.stringify on a cycle throws a different error — 'Converting circular structure to JSON'.)

Cause 5 of 6

Spreading an enormous array into a function call.

Math.max(...values) with hundreds of thousands of elements exceeds the argument limit rather than recursing. Use a reduce, or process the array in chunks.

Cause 6 of 6

Two functions calling each other indirectly.

The stack trace shows the repeating cycle — read the frames that repeat, not just the top one. Event handlers that dispatch the event they handle are a common form.

Stopping it coming back

Give every recursive function an explicit depth limit or an iterative form for deep inputs, and let a lint rule flag state updates written directly in a render body.

Maximum call stack size exceeded — common questions

How deep can the stack actually go?
It depends on the engine, the platform and how much each frame holds — roughly ten thousand frames is a common order of magnitude in browsers, and a function with many locals hits the wall sooner. Treat it as a limit to design around, not a number to rely on.
Does JavaScript have tail call optimisation?
The specification defines it, but Safari is the only major engine that shipped it. Do not rely on it — rewrite deep recursion as a loop with an explicit stack instead.
Why does the stack trace only show a few frames?
Browsers truncate long traces. In Chrome you can raise the limit with Error.stackTraceLimit = 100 before the error occurs, which is often enough to see the whole repeating cycle.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor