Dan Vanderkam
Aug 28, 2017 · 1 min read

This is exactly right! When I run a synchronous version of the iterator, I can process ~65MB/s. That’s still 1/3 the speed of the equivalent Python, but it’s in the ballpark. And the speed difference may be due to the way that TypeScript downlevels generators.

let numLines = 0, numBytes = 0;
for (const line of linesSync('stop_times.txt')) {
numLines++;
numBytes += line.length;
}
console.log(`Read ${numLines} lines, ${numBytes} bytes.`);

Thinking a little more about this, one thing I’ve learned from this post and the responses to it is that a knee-jerk “sync is bad” reaction isn’t always appropriate. If a server needs to read a huge CSV before serving requests, it may as well do so synchronously (and quickly).

This post prompted a discussion on the TC39 proposal GitHub. The gist is that they don’t think the semantics are broken, just that for-await-of should be much than it is. It’s hard to argue with that. If the simple for-await-of loop were as fast as the for-of equivalent, that would be the best of both worlds.

)

    Dan Vanderkam

    Written by

    Software Developer @sidewalklabs

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade