DEV Community

Discussion on: What is Recursion, and Why Shouldn't You Use It?

Collapse
 
shadowtime2000 profile image
shadowtime2000

People don't use recursion because they think it is fast. They usually use it because it fits with the functional programming paradigm which is growing more and more popular now a days.

Collapse
 
darthbob88 profile image
Raymond Price

Well that, and it's often simpler and clearer than the iterative method. Compare the two versions of tree traversal in this article.

Collapse
 
dmbaturin profile image
Daniil Baturin

It's a shame JS implementations still don't support tail call optimization. More and more JS is not written, but cross-compiled, and a lot of time the source language makes functional approach natural and easy to reason about.

Structural recursion is much easier to reason about than iteration, but you need TCO to make it fast, and most modern backends including .Net can already do it.

Collapse
 
darthbob88 profile image
Raymond Price

AFAIK ES2015 at the very least supports TCO as part of the spec, and allegedly the V8 implementation does support TCO, but I'm having a hard time finding any firm confirmation.