I've talked about tail recursion in a previous post, today I'll show an example on nodejs.
We have a function that sums numbers starting from 0 to an n number.
We have both version of it, the tail recursion and non-tail recursion version:
Use the following code to test the above two functions:
Run the functions using the following flags:
And here's the result:
The non-tail recursion version fail with "Maximum call stack size exceeded"
Note, if we run without the flags "--use-strict --harmony-tailcalls" the nodejs will run without tail recursion optimizations, and both will treated the same (will through the same error above).