3 ms·
On Recursion, Continuations and Trampolines
- michaelsbradley 10y agoIn 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/d1b104137e8e00468d83 https://gist.github.com/michaelsbradleyjr/d1b104137e8e00468d... 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.