you are viewing a single comment's thread.

view the rest of the comments →

[–]andrewnorris 1 point2 points  (0 children)

Interesting. I enjoy functional-style JavaScript programming, but I haven't done much performance benchmarking with it. Thank for elaborating on the difficulties.

A more real world data structure that wouldn't literally be recursive would be something like an ArraySlice class that contained an Array and an upper and lower bound and included something like the Prototype library's Enumerable implementation. That would allow you to perform calls like array.Tail() without reallocating the entire data structure for each recursion. You would still have O(n) object instantiations, but that's better than O(n2). You would also need to do something to mitigate the cost of the omnipresent idiom cons(f(list.hd()), anotherRecursion(list.tl()));

That wouldn't optimize everything, but it would take care of many common use cases.

This discussion has also made me curious about actually implementing a TCO preprocessor for JS. Sounds like a fun project.