heapwolf / prompt-sync

a synchronous prompt for node.js
MIT License
211 stars 42 forks source link

Use do-while instead of tail recursion #1

Closed minuteman3 closed 10 years ago

minuteman3 commented 10 years ago

Since javascript doesn't do tail call elimination a recursive solution still builds up a big pile of stack frames (one per character) each containing a string (ch) that doesn't get garbage collected until the whole thing has returned. It's trivial to replace tail recursion with a do-while loop which doesn't suffer from the negative effects.

minuteman3 commented 10 years ago

I suppose blocking the event loop isn't a great idea, maybe the fact that it's "sync" got me carried away.

heapwolf commented 10 years ago

yeah, even tough theres an unbound callstack it has a reasonably short lifetime :)