Quick answer: The JavaScript call stack ran out of room. Either a function kept calling itself (directly or through a chain of other functions) without returning, or a single call was passed more arguments than the stack can hold. Chrome and Safari say RangeError: Maximum call stack size exceeded; Firefox says InternalError: too much recursion. Find the repeating frames in the stack trace, then replace the recursion with a loop over an explicit stack, add a visited set, or chunk the oversized spread.

This error tends to arrive late in development. The flood fill worked on the test map, the scene graph traversal worked until a designer dragged one node onto another, the loop ran fine until a player left the tab in the background for ten minutes. A stack overflow in a game is almost always recursion whose depth depends on data: map size, list length, elapsed time. Below are the wordings in each browser, how to read the trace to see which function recurses and who started it, and broken and fixed code for the ways browser games usually get here.

The exact error

Uncaught RangeError: Maximum call stack size exceeded     (Chrome, Edge)
RangeError: Maximum call stack size exceeded.             (Safari)
Uncaught InternalError: too much recursion                (Firefox)

All three mean the same thing. It is an ordinary exception: it can be caught, the tab does not crash, and the stack unwinds normally. What stops is whatever was running. If that was your frame function, the game freezes on its last frame while the page stays responsive.

One portability detail: Firefox throws an InternalError, a type that only exists in Firefox, so err instanceof RangeError is false there. If you need to recognise the condition in code, test for both.

What the limit is

Every call that has not returned yet occupies a frame on a fixed-size stack. A frame’s size depends on the function: its arguments, its local variables, and how the engine compiled it. So the limit is a number of bytes, and the depth you get from it varies. In a current desktop Chrome, a function with no arguments recursed about 12,500 levels before overflowing, and a function with eight arguments and a few locals managed about 4,000. Other engines, phones and Web Workers give different numbers, and a browser update can change them.

That is why a depth that works on your desktop fails on a player’s phone. The rule to design by: recursion depth must be bounded by something small that you control (the nesting of a menu, the height of a balanced tree), never by the size of the data (tiles in a room, nodes in a list, seconds since the last frame).

There is a second way to overflow that involves no recursion at all: passing a very large array as individual arguments. That one is covered under spread and apply.

Read the stack before changing code

For recursion you keep, a depth counter turns an anonymous overflow into an error that names the data:

let depth = 0;
function visit(node) {
  if (++depth > 5000) {
    depth = 0;
    throw new Error("visit() is 5000 levels deep at node " + node.name);
  }
  try {
    for (const child of node.children) visit(child);
  } finally {
    depth--;
  }
}

Flood fill and other grid recursion

Region filling, room detection, lighting spread and match-3 group finding are all written recursively in tutorials, and all use one stack frame per tile:

// Broken: depth grows with the area of the region.
function fill(map, x, y, from, to) {
  if (x < 0 || y < 0 || x >= map.width || y >= map.height) return;
  if (map.get(x, y) !== from) return;
  map.set(x, y, to);
  fill(map, x + 1, y, from, to);
  fill(map, x - 1, y, from, to);
  fill(map, x, y + 1, from, to);
  fill(map, x, y - 1, from, to);
}

In a test on desktop V8 this filled an open 64 by 64 room and overflowed on an open 100 by 100 one. It also recurses forever when from equals to, because a filled tile still matches. Keep the pending tiles in an array instead of on the call stack:

// Fixed: constant call depth, any map size.
function fill(map, startX, startY, from, to) {
  if (from === to) return;
  const stack = [startX, startY];
  while (stack.length > 0) {
    const y = stack.pop();
    const x = stack.pop();
    if (x < 0 || y < 0 || x >= map.width || y >= map.height) continue;
    if (map.get(x, y) !== from) continue;
    map.set(x, y, to);
    stack.push(x + 1, y, x - 1, y, x, y + 1, x, y - 1);
  }
}

An array used this way is limited by memory, not by the call stack. If the fill is slow enough to drop frames, process a fixed number of tiles per frame and keep the array between frames.

Scene graphs, trees and pathfinding with a cycle

A recursive traversal of a tree is fine until the tree stops being one. The usual way that happens is reparenting a node under its own descendant, after which updateTransform, destroy or a serialiser walks the loop until the stack is gone. Refuse the cycle where it is created:

function setParent(node, parent) {
  for (let p = parent; p; p = p.parent) {
    if (p === node) {
      throw new Error("Cannot parent " + node.name + " under its own descendant " + parent.name);
    }
  }
  if (node.parent) node.parent.children.splice(node.parent.children.indexOf(node), 1);
  node.parent = parent;
  if (parent) parent.children.push(node);
}

For graphs that are allowed to contain cycles (navigation meshes, room connections, dependency lists), traverse with an explicit stack and a set of visited nodes:

function eachNode(root, visit) {
  const seen = new Set();
  const stack = [root];
  while (stack.length > 0) {
    const node = stack.pop();
    if (seen.has(node)) continue;
    seen.add(node);
    visit(node);
    for (const child of node.children) stack.push(child);
  }
}

The same applies to pathfinding. A recursive depth-first search with no closed set revisits tiles, and even with one its depth equals the path length. Use a queue or an open list, as breadth-first search and A* do.

A game loop that calls itself

Two loop bugs overflow the stack. The first is a typo that calls the function when you meant to pass it:

// Broken: loop() runs now, recursively, before requestAnimationFrame is ever called.
function loop() {
  update();
  render();
  requestAnimationFrame(loop());
}

// Fixed: pass the function itself.
function loop(time) {
  update(time);
  render();
  requestAnimationFrame(loop);
}
requestAnimationFrame(loop);

The second only appears in the wild. A fixed-timestep loop that catches up by recursion works while frames are short, then overflows when the player returns to a tab that was hidden, because the first frame back carries minutes of elapsed time:

// Broken: one stack frame per simulation step still owed.
function step() {
  if (accumulator >= STEP) {
    simulate(STEP);
    accumulator -= STEP;
    step();
  }
}

// Fixed: loop, and clamp how much time one frame may add.
function update(dt) {
  accumulator += Math.min(dt, 0.25);
  while (accumulator >= STEP) {
    simulate(STEP);
    accumulator -= STEP;
  }
}

Events, setters and state machines that re-enter

These show up as two or three alternating names in the trace.

Handlers that emit the event they listen to. A volume slider updates the settings object, the settings object emits a change, and a listener sets the slider, which emits again. Break the loop where the value is stored:

settings.setVolume = function (value) {
  if (value === this.volume) return;   // nothing changed, nothing to announce
  this.volume = value;
  this.emit("volume", value);
};

A setter or getter that uses its own name. this.hp inside set hp calls the setter again. Back the property with a different field:

// Broken
class Player {
  get hp() { return this.hp; }
  set hp(value) { this.hp = Math.max(0, value); }
}

// Fixed
class Player {
  #hp = 100;
  get hp() { return this.#hp; }
  set hp(value) { this.#hp = Math.max(0, value); }
}

State machines that transition inside enter(). If Idle.enter switches to Walk when a key is held and Walk.enter switches back to Idle when the path is blocked, both conditions being true at once produces an endless synchronous ping-pong. Record the requested state and apply it once per update, or make transition decisions in update() only.

Collision callbacks that re-run the collision check. A callback that moves a body and then calls the same overlap test that invoked it recurses for as long as the bodies still overlap. Resolve the overlap in the callback and let the next physics step run the test.

Too many arguments: spread and apply on big arrays

Math.max(...values), target.push(...source) and String.fromCharCode.apply(null, bytes) place every element on the stack as a separate argument. With a heightmap, a merged vertex buffer or the bytes of a save file, that is hundreds of thousands of arguments. In a current desktop Chrome these calls worked with 120,000 elements and threw RangeError: Maximum call stack size exceeded at 125,000; the cut-off is lower when the call happens deep in other calls, and differs by browser. Firefox words this case differently: RangeError: too many function arguments.

Nothing recursed, so the trace shows no repeating frames. Use a loop, or work in chunks:

function maxOf(values) {
  let max = -Infinity;
  for (let i = 0; i < values.length; i++) {
    if (values[i] > max) max = values[i];
  }
  return max;
}

function appendAll(target, source) {
  for (let i = 0; i < source.length; i++) target.push(source[i]);
  return target;
}

// Bytes to a binary string, for btoa() on a save file or screenshot.
function bytesToBinaryString(bytes) {
  const CHUNK = 0x8000;
  let text = "";
  for (let i = 0; i < bytes.length; i += CHUNK) {
    text += String.fromCharCode.apply(null, bytes.subarray(i, i + CHUNK));
  }
  return text;
}

Save data: deep copies and serialisation

A hand-written recursive deepCopy overflows on the first cycle it meets, and game state is full of them: an enemy holds its target, the target holds its attackers. structuredClone handles cycles correctly. JSON.stringify refuses them with a different error (TypeError: Converting circular structure to JSON in Chrome, cyclic object value in Firefox, JSON.stringify cannot serialize cyclic structures. in Safari), which is the cue to save ids instead of object references.

Depth is the other problem. An undo history where each state points to the previous one, or a linked list of replay frames, is nested as deep as it is long. In current Chrome, structuredClone on a chain 5,000 objects deep threw RangeError: Failed to execute 'structuredClone' on 'Window': Maximum call stack size exceeded, and older V8 versions overflow in JSON.stringify at similar depths. Since postMessage to a worker and IndexedDB use the same cloning algorithm, the fix belongs in the data: store long sequences as flat arrays of records.

What does not cause it

Recursion across an await or a timer. A function that calls itself after await, inside .then(), or through setTimeout and requestAnimationFrame starts each call on a fresh stack. A polling loop written as poll().then(poll) ran 200,000 rounds in a test without growing the stack. The exception is recursion before the first await: async function f() { await f(); } calls itself synchronously and overflows like any other function.

Missing tail-call optimisation is not something to fix in settings. ES2015 specified proper tail calls, but among browsers only Safari’s engine implements them, and only in strict mode. Tail-recursive code that runs to any depth there still overflows in Chrome and Firefox, so write the loop.

How to confirm the fix

Telling one stack overflow from another in reports

The Bugnet Web SDK files an uncaught stack overflow like any other exception: once per session, with the stack the browser provided, the last 50 console lines, a screenshot of the canvas, the user agent and the game version. Two limits apply. The browser’s trace holds only the newest frames, so the caller that started the recursion is usually missing. And reports are grouped by title, which here is the browser’s generic message, so a flood fill overflow and a scene graph cycle stack onto the same entry. A depth guard solves both: an error such as fill() passed 50000 tiles on level 3-2 has its own title, names the data, and gets its own occurrence count in the dashboard. The SDK docs cover setup.

Frequently asked questions

What does Maximum call stack size exceeded mean?

The JavaScript engine ran out of stack space. A function called itself, or a group of functions called each other, too many times without returning, or one call received too many arguments. The engine throws a RangeError and unwinds the stack; the tab keeps running.

What is the maximum call stack size in Chrome?

There is no fixed number of calls. The limit is a size in bytes and each function uses a different amount. In a current desktop Chrome a trivial function recursed about 12,500 levels and a function with eight arguments about 4,000. Phones, workers and other browsers differ.

Why does Math.max with a spread array throw Maximum call stack size exceeded?

Spreading passes every element as a separate argument, and arguments live on the call stack. Past roughly a hundred thousand elements in Chrome the call no longer fits. Use a loop or reduce to find the maximum, and process large arrays in chunks.

Why does Firefox say too much recursion instead?

Firefox reports the same condition as InternalError: too much recursion. InternalError is a Firefox-only error type, so a check for RangeError does not match it. The causes and fixes are identical to the Chrome and Safari error.

Can I increase the call stack size in the browser?

Not for players. The limit is set by the browser and the device. Fix the code instead: replace data-dependent recursion with a loop over an explicit stack or queue, detect cycles with a visited set, and avoid spreading large arrays into function calls.

If the recursion depth is decided by the size of a map, a list or a time gap, it will overflow on some player’s device. Move the pending work from the call stack into an array.