3 ms·
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 int
by woooooo 3y 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?
- orwin 3y agoIn 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.