From 0a85b0fd9de0e4031f85db7548700f537dda832e Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 22 Dec 2023 19:34:17 +0100 Subject: [PATCH] lib: reduce overhead of `SafePromiseAllSettledReturnVoid` calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/pull/51243 Reviewed-By: Michaël Zasso Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: James M Snell --- 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 64438ddd5219f1..2c207d451d8450 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -569,9 +569,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