From 7ce04bfb09778c45c08acc2a8dba4fded4b0d626 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 20 Dec 2023 19:21:40 +0100 Subject: [PATCH] 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. --- lib/internal/per_context/primordials.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index c4f2b12aff2cba..5deea076755c9e 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -570,9 +570,17 @@ primordials.SafePromiseAllSettled = (promises, mapFn) => * @param {(v: T|PromiseLike, k: number) => U|PromiseLike} [mapFn] * @returns {Promise} */ -primordials.SafePromiseAllSettledReturnVoid = async (promises, mapFn) => { - await primordials.SafePromiseAllSettled(promises, mapFn); -}; +primordials.SafePromiseAllSettledReturnVoid = (promises, mapFn) => new Promise((resolve) => { + let pendingPromises = promises.length; + if (pendingPromises === 0) resolve(); + const onSettle = () => { + if (--pendingPromises === 0) resolve(); + }; + for (let i = 0; i < promises.length; i++) { + const promise = mapFn != null ? mapFn(promises[i], i) : promises[i]; + PromisePrototypeThen(PromiseResolve(promise), onSettle, onSettle); + } +}); /** * @template T,U