The maximum call stack size

Are you curious how many recursive calls you can make on your JavaScript engine?

How many recursive calls?

The following function lets you find out (inspired by a gist by Ben Alman).
    function computeMaxCallStackSize() {
        try {
            return 1 + computeMaxCallStackSize();
        } catch (e) {
            // Call stack overflow
            return 1;
Three results:
  • Node.js: 11034
  • Firefox: 50994
  • Chrome: 10402
What does this number mean? Mr. Aleph pointed out to me that on V8, the number of recursive calls you can make depends on two quantities: the size of the stack and the size of the stack frame (holding parameters and local variables). You can verify the latter by adding local variables to computeMaxCallStackSize() – it’ll return a lower number.

Tail call optimization in ECMAScript 6

ECMAScript 6 will have tail call optimization: If a function call is the last action in a function, it is handled via a “jump”, not via a “subroutine call”. That means that, if you slightly rewrote computeMaxCallStackSize(), it would run forever under ECMAScript 6 (in strict mode):
    function computeMaxCallStackSize(size) {
        size = size || 1;
        return computeMaxCallStackSize(size + 1);


Axel Rauschmayer said...

Funny :). You couldn't use it to calculate the size in any environment. In ES6, it continues indefinitely (and therefore never returns anything), and all other contexts it's a RangeError (and therefore never returns anything).

Axel Rauschmayer said...

http://jsfiddle.net/9YMDF/9/ visualizes call stack size depending on amount of args/vars in called function

Axel Rauschmayer said...

I think you are confusing the File API with the Filesystem and FileWriter API which is only supported by Chrome.

Axel Rauschmayer said...

Yes, wasn’t aware those two weren’t part of the File API.

Axel Rauschmayer said...

cool, nice!

Axel Rauschmayer said...

You can compute the call size by knowing max recursion number and local variable size: http://bahmutov.calepin.co/javascript-stack-size.html