Skip to content

Comment on Hacking Coroutines into Cparent

Comments

One of the most surprising things I discovered a while back is that this technique not only technically works in JavaScript, it actually beats its own built-in generator syntax in performance terms even if used as an iterable object:

https://jsbenchit.org/?src=1b165435c816c6d298e6b800b4742568

https://jsbenchit.org/?src=dedb07499cfa289b94d686bde05901df

Context: JS has an iteration protocol[0] that lets you create your own custom objects to be used with syntactic sugar for iteration. The sensible expectation is that the built-in syntax for generating such functions would produce the fastest code. It clearly doesn't.

Having said that I do not recommend manually writing code this way because if this is something that you need to worry about while writing JavaScript, it's a sign that you're using the wrong tool for the job anyway.

[0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.