Echo JS 0.11.0

<~>
zhirzh 311 days ago. link parent 1 point
that's not really helpful - you are dodging the "problem at hand".

i am asking whether or not to use setTimeout to perform recursion.
i know that deep recursions freeze the browser, so setTimeout is a POSSIBLE solution. another thing that can be done is to break the operation in batches of a few and use setTimeout to perform them in async.

some points for or against the approach, or perhaps some tips on how to deal with heavy recursions

Replies

MaxArt 311 days ago. link 1 point
I'm dodging the problem because that's what you probably should do. Deep recursion brings a lot of problems in JavaScript - first of all, the stack's depth.

But you don't seem to even need that stack, since you're ready to use setTimeout, discarding the closure's scope: that's actually a clear sign that you should use another approach, probably iterative. That would give you a good performance boost too.

That's why using setTimeout is generally frowned upon: it's a hack to get over a language limitation that could be solved with other, more efficient techniques.

So I don't recommend using setTimeout, because I don't recommend using recursion in the first place. Tail call optimization, where available, could solve the depth problem but it won't yield a performance as good as an iterative approach.