Skip to content

Commit 0a85b0f

Browse files
aduh95richardlau
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 9b25268 commit 0a85b0f

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

lib/internal/per_context/primordials.js

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

576584
/**
577585
* @template T,U

0 commit comments

Comments
 (0)