Course contentsShow
← JavaScript
Lesson 2064 of 2,72342. Iteration Protocols Deep DiveFree lesson

Async Iterator Overhead and Microtask Scheduling

Examine the performance impact of async iteration and how each await introduces microtask queue overhead.

Async Iterator Overhead and Microtask Scheduling

You'll learn how async iteration introduces performance costs through microtask queue scheduling that sync iteration avoids entirely.

The Microtask Tax of Async Iteration

Every time you use await in an async iterator (including within for-await-of loops), JavaScript doesn't just pause—it schedules a microtask. Each await statement adds work to the microtask queue, which the event loop must process before continuing.

Think of it like checking your mailbox: synchronous iteration is like walking directly to your destination, while async iteration is like stopping at the mailbox after every single step. Even if the mailbox is empty (the promise is already resolved), you still paid the cost of checking.

The Performance Impact

When you iterate with for-await-of over an async generator or async iterable, each iteration involves:

  1. Calling the async iterator's next() method
  2. Receiving a promise (even if instantly resolved)
  3. Scheduling a microtask to handle that promise
  4. Waiting for the microtask queue to drain
  5. Resuming execution

This happens per iteration. With thousands of items, those microtask overhead costs compound significantly compared to synchronous iteration, which executes directly without any queue scheduling.

When It Matters

For small datasets or truly asynchronous operations (network requests, file I/O), this overhead is negligible compared to the I/O wait time. But for large datasets where values are already available synchronously, async iteration can be 10-100x slower than sync iteration due purely to microtask scheduling overhead.

Key Takeaway: Async iteration has inherent microtask scheduling overhead at every step; use it when you need asynchronous coordination, not as a default choice for all iteration.