logoalt Hacker News

conartist6 • today at 1:22 PM • 0 replies • view on HN

That JS supports await isn't nearly as much a problem as the fact that it defensively defers execution any time it encounters one.

This hurts most badly for iterators.

Files are usually read from disks in chunks of 64K bytes. If you present the data as an async byte iterator you'll get 65,534 defensive deferrals, and one actual wait.

If you have ten layers of transformation it will be 655,350 defensive deferrals and 10 real waits.

These defensive deferrals aren't so expensive in terms of CPU time, but they ARE very expensive in terms of wall clock time. All other work must be halted so the event loop code can run. Eventually synthetic deferrals dominate the wall clock cost of running the program. This isn't hypothetical either. Here's someone trying to build steam parsing on Node and finding that it's too slow to be useful: https://github.com/nodejs/node/issues/31979

The solution is stream iterators. They are sync when they can be and async when they must be: https://docs.bablr.org/architecture/spacetime