7. Popping the Hood
Activity 7.01: Finding out the Number of Stack Frames
Solution
- The function that establishes the call stack's limit is as follows:
var frameCount = 0;
function stackOverflow() { frameCount++; stackOverflow(); }The solution starts out with the
frameCountvariable being initialized with the value0. ThestackOverflow()function is declared, which will add 1 to theframeCountvariable and then call itself, thus causing a stack overflow. - Now,
setTimeout()function is initiated, which will log the value offrameCountto the console after a minimum of 500 milliseconds. Now, call thestackOverflow()function.setTimeout(() => console.log(frameCount), 500); stackOverflow();
This takes the
console.logfunction out of the main execution thread, allowing it to be called after the stack overflow error is thrown:
Figure 7.22: Showing the solution and number of stack frames being pushed before a stack overflow is triggered
...