0

index.js:

function factorial(n, total) {
  if (n === 1) return total;
  return factorial(n - 1, n * total);
}

console.log(factorial(70000, 1))

And I run it like this:

node --harmony_tailcalls index.js      

But it still give me an error:

function factorial(n, total) {                                                                                                                                                                                                                          
              ^                                                                                                                                                                                                                                     

RangeError: Maximum call stack size exceeded  

why? I read this Node.js tail-call optimization: possible or not? , but still not work.

Community
  • 1
  • 1
Lin Du
  • 88,126
  • 95
  • 281
  • 483
  • Since this is a duplicate of the question TJ answered already, here's the answer that targets your scenario specifically; http://stackoverflow.com/a/39700867/2879498 – TylerY86 Sep 26 '16 at 10:56
  • 1
    The full answer is TJ's definitive answer; http://stackoverflow.com/a/30369729/2879498 – TylerY86 Sep 26 '16 at 10:57
  • Basically, if you're using Node v6.6.0, you just need the `--harmony` flag, and you need to be using strict mode (`"use strict";`). – T.J. Crowder Sep 26 '16 at 11:01
  • @T.J.Crowder Thanks! After add `"use strict";` and run `node --harmony index.js`, it seems works. The result is `Infinity`. Is it correct? After I check the call stack. I think it work correct. There is just one my function in the call stack. – Lin Du Sep 27 '16 at 01:40
  • @T.J.Crowder How TCO work in browser? Thanks again! – Lin Du Sep 27 '16 at 01:44
  • 1
    @novaline: You'll have to check per browser. Someday, when engines have a mature TCO feature, it'll just be there. For now, if it's there at all, it'll probably be hiding behind a flag you have to enable (if even that's available). – T.J. Crowder Sep 27 '16 at 02:38

0 Answers0