0

When I run the function below, I get different results on different browsers:

function maxCallStackSize0() {
    try {
        return 1 + maxCallStackSize0();
    } catch (e) {
        return 1;
    }
}
maxCallStackSize0(); // Opera: 32354, Chrome: 12795

But that's not all. The result also changes if I run it manually multiple times:

maxCallStackSize0(); // Opera: 34724
maxCallStackSize0(); // Opera: 33776
maxCallStackSize0(); // Opera: 34030

Is that because of other functions being called in the background taking up some of the stack?

I have also seen that the more arguments I pass, the smaller the call stack:

function maxCallStackSize3(s1, s2, s3, s4) {
    try {
        return 1 + maxCallStackSize3(s1, s2, s3, s4);
    } catch (e) {
        return 1;
    }
}
maxCallStackSize3("hello", "how", "are", "you"); // Opera: 13979, Chrome: 6971

Is that because the parameters are part of the call stack, and the more/the bigger size parameters I pass, the shorter the call stack can be before it overflows?

Is it possible to know the maximum size of the call stack in bytes?

Thanks

user332336
  • 81
  • 5
  • 1
    The actual exception is `RangeError: Maximum call stack size exceeded`, and yes, it depends on how the specific instance of browser is used. And yes, the more params, the more memory is reserved in stack. – raina77ow Feb 20 '19 at 16:21
  • Related: https://stackoverflow.com/questions/1248302/how-to-get-the-size-of-a-javascript-object – Eriks Klotins Feb 20 '19 at 16:22
  • @raina77ow And it depends on the number of parameters, not on their size, right? I assume the stack stores an address to the heap where it can find the right value – user332336 Feb 20 '19 at 16:40

1 Answers1

1

The stack size depends on multiple things here.

Firstly, when you call a function, all the parameters and a return address are pushed to the stack. Also all local variables that you would define within the function would be on the stack.

Secondly, the stack grows in the opposite direction as the heap. A process uses one memory space for the stack and the heap. If you have variables on the heap (for example if you instantiate a variable with new it will be created on the heap, not on the stack). The more on the heap, the less room there is for the stack to grow into.

I don't know how your setup looks like, but if there is anything else running in your browser-tab than that will interfere with the stack-size as it will take up some space.

user3637541
  • 675
  • 4
  • 15
  • Thanks. You say the parameters and local variables get pushed to the stack when a function is called. Is it the actual parameters, or just a reference to them? If the actual variables are added, it could fill the stack very quickly! – user332336 Feb 20 '19 at 17:03
  • 1
    Javascript is pass by reference. That means, you pass the actual value for primitives (like numbers) but objects are passed as a reference. The object itself is not passed. This does not contribute to filling the stack or not though, because the objects are stored on the heap and as the heap grows within the same limited memory space your possible stack size still shrinks, anyways. – user3637541 Feb 21 '19 at 01:00