Re: [whatwg/streams] ReadableStream.from(asyncIterable) (#1083)

To be clear, I was only trying to evaluate the performance of treating Array as a special case, I was not trying to make the behaviour match the async iterator case.

Having thought about it a bit more, making Array a special case is not really justifiable since as @domenic noted it gets weird if someone has tampered with the object or the prototype.

So I've experimented with making sync iterators a special case instead. As with the Array case, I enqueue everything synchronously when the stream is created. And as with the Array case, this means it will fill up your memory and crash if you put an infinite iterator in there.

The performance is actually weirdly good. I'm getting a median of 82μs, which is within the margin of error of the Array special case. I can't explain why it's so fast, but I'm not complaining.

Here's what I'm proposing:

1. Check if _asyncIterable_ has an `@@iterator` method. If it does then call GetIterator(_asyncIterable_, sync) and synchronously enqueue all the values in the startAlgorithm.
2. Otherwise, call GetIterator(_asyncIterable_, async) and lazily pull items as before.
3. Tell people that if they want to use an infinite iterator with `from()` they have to make it async.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/pull/1083#issuecomment-881072145

Received on Thursday, 15 July 2021 23:27:18 UTC