title: Limiting Recursion Depth in NodeJS
body: <p>Is there a way to limit the <strong>depth</strong> of recursion in NodeJS other than the <code>--stack-size</code> option?  </p>
  
  <p>I know that NodeJS will let you control the size of you call stack via the <code>--stack-size</code> option.  i.e. a program like this</p>
  
  <pre><code>const recurse = (num=0) =&gt; {
    context.num++
    recurse(++num)
  }
  const context = {
    num:0
  }
  try {
    recurse(context)
  } catch(e) {
    console.log('Took ' + context.num + ' trips before bailing')
  }
  </code></pre>
  
  <p>will output something like this by default.</p>
  
  <pre><code>$ node so.js 
  Took 12530 trips before bailing
  </code></pre>
  
  <p>However, you can use <code>--stack-size</code> 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. </p>
  
  <pre><code>$ node --stack-size=5000 so.js 
  Took 63935 trips before bailing
  </code></pre>
  
  <p>The <code>--stack-size</code> option is one of Node's V8 options.  You can see a list of all these options by running <code>node --v8-options</code>.</p>
  
  <p>What I want to know is whether there's a way in NodeJS to explicitly limit the recursion depth by some specific number. </p>
  
  <p>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 :)</p>