From f589ec9bfed04fdc7e54d4bc88e5fe1f63b0a78b Mon Sep 17 00:00:00 2001 From: Amy Date: Tue, 28 Jun 2022 00:25:32 -0400 Subject: [PATCH] Create new ImagePool for each batch --- scripts/optimize-images.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/optimize-images.ts b/scripts/optimize-images.ts index 803c54ee..ba4775e8 100644 --- a/scripts/optimize-images.ts +++ b/scripts/optimize-images.ts @@ -44,12 +44,14 @@ export async function optimizeImages() { // maximum number of workers is 8 in order to avoid running out of memory const numberOfWorkers = Math.min(cpus().length, 8); - const imagePool = new ImagePool(numberOfWorkers); // process smaller batches in order to reduce memory usage const batchSize = 32; for (let i = 0; i < imagePaths.length; i += batchSize) { + // use a new ImagePool for each batch to reduce memory usage + const imagePool = new ImagePool(numberOfWorkers); + await Promise.all( imagePaths.slice(i, i + batchSize).map(async (imagePath) => { const imageStartTime = Date.now(); @@ -108,9 +110,9 @@ export async function optimizeImages() { ); }) ); - } - await imagePool.close(); + await imagePool.close(); + } console.log(`TOTAL DURATION: ${getElapsedSeconds(startTime)}s`); }