From b65799c809ec7badc9cdf051e3f5feb078a6ab20 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 20 Jun 2025 11:41:56 -0300 Subject: sugar: queue: kick start --- src/common-util/sugar.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/common-util/sugar.js b/src/common-util/sugar.js index aeb0fe4f..c7e15dd3 100644 --- a/src/common-util/sugar.js +++ b/src/common-util/sugar.js @@ -360,13 +360,27 @@ export function queue(functionList, queueSize = 50) { thisReject(error); } finally { running--; - next(); + + // If the cursor is at 1, this is the first promise that resolved, + // so we're now done the "kick start", and can start the remaining + // promises (up to queueSize). + if (cursor === 1) { + // Since only one promise is used for the "kick start", and that one + // has just resolved, we know there's none running at all right now, + // and can start as many as specified in the queueSize right away. + for (let i = 0; i < queueSize; i++) { + setTimeout(next); + } + } else { + setTimeout(next); + } } }; - for (let i = 0; i < queueSize; i++) { - next(); - } + // Only start a single promise, as a "kick start", so that it resolves as + // early as possible (it will resolve before we use CPU to start the rest + // of the promises, up to queueSize). + next(); return promiseList; } -- cgit 1.3.0-6-gf8a5