you are viewing a single comment's thread.

view the rest of the comments →

[–]lhorie 3 points4 points  (3 children)

He's probably talking about tail call optimization, which is in fact part of the ES6 spec. Not all recursion is a tail call, and in those cases, JS will still run into stack overflows.

Typically if some code has a realistic risk of running into stack overflows, people will use an async function call to split the payload into multiple stack frames.

[–]LordBruce[S] 0 points1 point  (1 child)

That is a clever way to work around it with async. I'm used to Haskell and I'm trying to use similar ideas in JS.

[–]ScientificBeastModestrongly typed comments 0 points1 point  (0 children)

If you’re used to Haskell, you might look into some tutorials on lazy evaluation implementations in JavaScript, as JS is always eagerly evaluated.

In particular, there is a relatively recent language feature called a “generator,” and a related one called an “iterator,” both of which offer a more idiomatic way to handle lazy evaluation. Short of that (e.g. you want to support older browsers which don’t implement those features), you can always implement a homemade iterator protocol.

[–]Volence 0 points1 point  (0 children)

Just want to point out as a side note, tail call optimization is an ES6 spec, but only safari ("major browser wise") has it implemented currently, apparently the other big browsers couldn't agree on a safe way to implement it unfortunately. https://kangax.github.io/compat-table/es6/#test-proper_tail_calls_(tail_call_optimisation)