Is there a way to limit the depth of recursion in NodeJS other than the --stack-size
option?
I know that NodeJS will let you control the size of you call stack via the --stack-size
option. i.e. a program like this
const recurse = (num=0) => {
context.num++
recurse(++num)
}
const context = {
num:0
}
try {
recurse(context)
} catch(e) {
console.log('Took ' + context.num + ' trips before bailing')
}
will output something like this by default.
$ node so.js
Took 12530 trips before bailing
However, you can use --stack-size
to tell node how big you want to let the stack get (in kb) before bailing out. Use a bigger stack, you get more recursion.
$ node --stack-size=5000 so.js
Took 63935 trips before bailing
The --stack-size
option is one of Node's V8 options. You can see a list of all these options by running node --v8-options
.
What I want to know is whether there's a way in NodeJS to explicitly limit the recursion depth by some specific number.
I don't have a specific end goal in mind here -- I'm just trying to understand what tools NodeJS does and does not have compared to other languages I've works in. It's OK if node doesn't have this -- I'm just learning here :)