Skip to content

Commit

Permalink
Avoid unnecessary delay in pMapIterable
Browse files Browse the repository at this point in the history
Eagerly invoke iterator.next() even when already at concurrency limit.

Otherwise, when a slot frees up and we have the chance to invoke the
mapper, we first have to wait for iterator.next() to resolve. During
this time the free slot is unused.

With this change the resolution of iterator.next() will already have
started and there is at least a chance that it's ready by the time we
want to invoke the mapper.

In principle, one could even implement a configurable backlog for
iterator.next() results. That would be a bigger change though.

Fixes sindresorhus#78
  • Loading branch information
dmitri-gb committed Aug 12, 2024
1 parent 088f1a5 commit 8df3b16
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export function pMapIterable(
async * [Symbol.asyncIterator]() {
const iterator = iterable[Symbol.asyncIterator] === undefined ? iterable[Symbol.iterator]() : iterable[Symbol.asyncIterator]();

let nextIterResult = iterator.next();
const promises = [];
let pendingPromisesCount = 0;
let isDone = false;
Expand All @@ -202,7 +203,10 @@ export function pMapIterable(

const promise = (async () => {
pendingPromisesCount++;
const {done, value} = await iterator.next();

const iterResult = nextIterResult;
nextIterResult = iterator.next();
const {done, value} = await iterResult;

if (done) {
pendingPromisesCount--;
Expand Down

0 comments on commit 8df3b16

Please sign in to comment.