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

> Is there no room for just optimizing array iterables more? I know SpiderMonkey has a specific [optimization for iterating over arrays](https://searchfox.org/mozilla-central/source/js/src/vm/ForOfIterator.cpp#101).

It looks like [`ForOfIterator`](https://searchfox.org/mozilla-central/rev/77f0b36028b2368e342c982ea47609040b399d89/js/public/ForOfIterator.h#29-49) is only meant to imitate the behavior of a `for..of` loop? For `ReadableStream.from()`, we need the behavior of a `for await..of` loop instead, so you'd still have to wrap it with `CreateAsyncFromSyncIterator`.

---

I don't think it's a valid optimization to *exhaustively* iterate an array. For such optimization to be unobservable, you'd have to prove that the array can never be mutated after constructing the stream, and that all of its elements are non-thenables (so they won't trigger a delay when using `it.next()`). This seems like too complex of a condition to check at runtime.

If I understand the code from the CL correctly, you already have special handling for synchronous iterators in `ScriptAsyncIterator`. Perhaps it can also optimize for the common case where `it.next().value` is a non-thenable, and thus `AsyncFromSyncIteratorContinuation` doesn't need to attach an `UnwrapFunction`? Maybe that can save a microtask? Or is the overhead not coming from using too many microtasks?

-- 
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-881058649

Received on Thursday, 15 July 2021 22:49:42 UTC