Re:All About Recursion and Tail Calls in JavaScript
by xg15 on Sunday May 14, 2017 @06:31AM
(#14335031)
> You absolutely should have knowledge of your target platform and whether it does TCO
If I write web apps, as far as I'm concerned, my platform is V8, Whatevermonkey, Trident and every other hypothetical javascript engine out there, any of which might or might not support TCO under different circumstances.
> When in doubt, don't use recursion and don't expect that you get TCO.
I think relying on TCO is always a bad idea, exactly because you can't be sure your platform supports it. But STC doesn't change that. You can't expect the platform to support tail calls because you did some magic incantations.
On the other hand, it doesn't make sense to me to forbid tail calls either. If you know your recursive code will work without TCO, it will work with TCO, too.
Otherwise, you may decide to just use recursion everywhere and then your stack overflows when your N is bigger than expected.
When in doubt, don't use recursion and don't expect that you get TCO.