
On Recursion, Continuations and Trampolines - ingve
http://eli.thegreenplace.net/2017/on-recursion-continuations-and-trampolines/
======
michaelsbradley
In node.js (v8) at least, generators introduce substantial CPU overhead (but
not much memory overhead) compared to trampolines. Trampolines, in turn, are a
good bit slower than straight recursion. There seems to be robust TCO in
recent releases, or support for extremely deep stacks, at least. Try this with
a recent release (see the comment at the bottom of the script):

[https://gist.github.com/michaelsbradleyjr/d1b104137e8e00468d...](https://gist.github.com/michaelsbradleyjr/d1b104137e8e00468d83)

Despite the overhead, using generator `yield` semantics (giving args to pass
to an indicated function, instead of calling directly, and then resuming
execution with a value provided by iter.next) one can "salvage" algorithms
that are cleanly expressed with non-tail recursive calls between, say, a
handful of functions. It takes a little work to setup the state machine to
serve as the plumbing, but it's not too difficult really.

