From 240b5c083106e87067f42e98ef89b8ab8a203ea5 Mon Sep 17 00:00:00 2001 From: Orlando Date: Fri, 13 Sep 2024 15:15:13 +0100 Subject: [PATCH] idealChunkDuration --- CHANGELOG.md | 6 +++++ package.json | 2 +- src/helpers/chunkedDynamic.ts | 43 ++++++++++++++++------------------- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b04835a..41c9546 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # deverything +## 1.5.2 + +### Patch Changes + +- idealChunkDuration + ## 1.5.1 ### Patch Changes diff --git a/package.json b/package.json index 5fe7baa..6e43b4f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deverything", - "version": "1.5.1", + "version": "1.5.2", "description": "Everything you need for Dev", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/src/helpers/chunkedDynamic.ts b/src/helpers/chunkedDynamic.ts index 06ef73a..b6e6c3c 100644 --- a/src/helpers/chunkedDynamic.ts +++ b/src/helpers/chunkedDynamic.ts @@ -1,3 +1,4 @@ +import { clamp } from "./clamp"; import { sleep } from "./sleep"; /** @@ -5,22 +6,25 @@ import { sleep } from "./sleep"; * @param array - Array to chunk * @param initialChunkSize - Size of initial chunk * @param fn - Function to run on each chunk - * @param options.sleepTime - Time to sleep between each chunk - * @param options.maxChunkDuration - Time to sleep between each chunk - * @param options.chunkSizeStep - Step to increase/decrease chunk size (works only if maxChunkDuration is set) + * @param options.idealChunkDuration - Ideal time to process each chunk, the chunk size will adjust to meet this duration + * @param options.sleepTimeMs - Time to sleep between each chunk + * @param options.maxChunkSize - Maximum chunk size (default 200) + * @param options.minChunkSize - Minimum chunk size (default 1) */ export const chunkedDynamic = async ( array: T[], initialChunkSize: number, fn: (chunk: T[], currentChunkIndex: number) => Promise, { - sleepTime, - maxChunkDuration, - chunkSizeStep, + idealChunkDuration, + maxChunkSize, + minChunkSize, + sleepTimeMs, }: { - sleepTime?: number; - maxChunkDuration?: number; - chunkSizeStep?: number; + idealChunkDuration?: number; + maxChunkSize?: number; + minChunkSize?: number; + sleepTimeMs?: number; } = {} ): Promise => { const chunkResults: any[] = []; @@ -28,27 +32,20 @@ export const chunkedDynamic = async ( let currentIndex = 0; let chunkIndex = 0; let currentChunkSize = initialChunkSize; - const step = chunkSizeStep || 1; while (currentIndex < array.length) { const start = performance.now(); const chunk = array.slice(currentIndex, currentIndex + currentChunkSize); const chunkResult = await fn(chunk, chunkIndex); chunkResults.push(chunkResult); - if (sleepTime) await sleep(sleepTime); - if (maxChunkDuration) { + if (sleepTimeMs) await sleep(sleepTimeMs); + if (idealChunkDuration) { const duration = performance.now() - start; - if (duration > maxChunkDuration) { - if (currentChunkSize <= 1) - // < is just for safety, should not go below 1 - await sleep(duration - maxChunkDuration); // Sleep the remaining time - else { - if (currentChunkSize - step < 1) currentChunkSize = 1; - else currentChunkSize -= step; // decrease chunk size to slow down - } - } else { - currentChunkSize += step; // increase chunk size to speed up - } + currentChunkSize = clamp({ + number: Math.round(currentChunkSize * (idealChunkDuration / duration)), + min: minChunkSize || 1, + max: maxChunkSize || 200, + }); } currentIndex += currentChunkSize;