In Chrome and Node, the following code throws an error:
function noop() {}
var a = new Array(1e6)
// Array[1000000]
noop.apply(null, a)
// Uncaught RangeError: Maximum call stack size exceeded
I understand why it might be a Bad Idea to pass 1 million arguments to a function, but can anyone explain why the error is Maximum call stack size exceeded
instead of something more relevant?
(In case this seems frivolous, the original case was Math.max.apply(Math, lotsOfNumbers)
, which is a not-unreasonable way of getting the max number from an array.)
The most common source for this error is infinite recursion. You must have a recursive function in your code whose base case is not being met and is, therefore, calling the function again and again until you hit the call stack limit.
The JavaScript exception "too much recursion" or "Maximum call stack size exceeded" occurs when there are too many function calls, or a function is missing a base case.
It means that somewhere in your code, you are calling a function which in turn calls another function and so forth, until you hit the call stack limit. This is almost always because of a recursive function with a base case that isn't being met.
Without any local variables, each function call takes up 48 bytes during the execution, and you are limited to less than 1MB for all local function frames. Each boolean and number variable takes 8 bytes of memory.
Function arguments are put on the stack. You're trying to put a million arguments onto the stack, and that's more than the maximum stack size. So the error message is very relevant to the reason for the error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With