Skip to content

Commit ed7305e

Browse files
aduh95RafaelGSS
authored andcommitted
lib: reduce overhead of SafePromiseAllSettledReturnVoid calls
It was simply calling `SafePromiseAllSettled`, which itself calls `arrayToSafePromiseIterable` which wraps all promises into new `SafePromise` object and wraps it into a `SafeArrayIterator`. Since we don't care about the return value, we can take some shortcuts. PR-URL: #51243 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 96d64ed commit ed7305e

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

lib/internal/per_context/primordials.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,17 @@ primordials.SafePromiseAllSettled = (promises, mapFn) =>
570570
* @param {(v: T|PromiseLike<T>, k: number) => U|PromiseLike<U>} [mapFn]
571571
* @returns {Promise<void>}
572572
*/
573-
primordials.SafePromiseAllSettledReturnVoid = async (promises, mapFn) => {
574-
await primordials.SafePromiseAllSettled(promises, mapFn);
575-
};
573+
primordials.SafePromiseAllSettledReturnVoid = (promises, mapFn) => new Promise((resolve) => {
574+
let pendingPromises = promises.length;
575+
if (pendingPromises === 0) resolve();
576+
const onSettle = () => {
577+
if (--pendingPromises === 0) resolve();
578+
};
579+
for (let i = 0; i < promises.length; i++) {
580+
const promise = mapFn != null ? mapFn(promises[i], i) : promises[i];
581+
PromisePrototypeThen(PromiseResolve(promise), onSettle, onSettle);
582+
}
583+
});
576584

577585
/**
578586
* @template T,U

0 commit comments

Comments
 (0)