Hacker News new | ask | show | jobs
by woooooo 1081 days ago
You have a cite or link exploring this?

I had thought tail-call optimization was a way to get inefficient recursion to parity with iteration, by turning it into iteration. I'd never heard of recursion being faster on its own.

Is it a javascript-specific thing due to how their int types work?

1 comments

In von Neumann architectures, iteration will always be faster than recursion, because it'll need way less machine code. 2 caveats:

1rst one: It really depends on your data structure. If you use a tree, recursion will be faster on most operations (unless you optimize like crazy, but tbh, it's never worth it). Even with simple linked lists, some operations are better written with recursion.

2nd: more often than not, using first order functions will be both more efficient and easier to read than a for loop. If you're using a foreach loop to apply a function to each items, please use a map.