Skip to content

Commit

Permalink
idealChunkDuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Sep 13, 2024
1 parent bf4d071 commit 240b5c0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# deverything

## 1.5.2

### Patch Changes

- idealChunkDuration

## 1.5.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
43 changes: 20 additions & 23 deletions src/helpers/chunkedDynamic.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,51 @@
import { clamp } from "./clamp";
import { sleep } from "./sleep";

/**
* @description Run a series of (async) functions with dynamic chunk sizes
* @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 <T>(
array: T[],
initialChunkSize: number,
fn: (chunk: T[], currentChunkIndex: number) => Promise<any>,
{
sleepTime,
maxChunkDuration,
chunkSizeStep,
idealChunkDuration,
maxChunkSize,
minChunkSize,
sleepTimeMs,
}: {
sleepTime?: number;
maxChunkDuration?: number;
chunkSizeStep?: number;
idealChunkDuration?: number;
maxChunkSize?: number;
minChunkSize?: number;
sleepTimeMs?: number;
} = {}
): Promise<any[]> => {
const chunkResults: any[] = [];

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;
Expand Down

0 comments on commit 240b5c0

Please sign in to comment.