From a6bc1ec256b14cc9bc83a7494f7c12ee6cbdae84 Mon Sep 17 00:00:00 2001 From: Amy Date: Sat, 4 Jun 2022 18:07:33 -0400 Subject: [PATCH] Try Promise.all on batches of 16 --- scripts/optimize-images.ts | 98 ++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 47 deletions(-) diff --git a/scripts/optimize-images.ts b/scripts/optimize-images.ts index bf2b3d64..b0a43a6c 100644 --- a/scripts/optimize-images.ts +++ b/scripts/optimize-images.ts @@ -46,64 +46,68 @@ export async function optimizeImages() { const numberOfWorkers = Math.min(cpus().length, 8); const imagePool = new ImagePool(numberOfWorkers); - await Promise.all( - imagePaths.map(async (imagePath) => { - const imageStartTime = Date.now(); + const batchSize = 16; - const sourcePath = path.join(SOURCE_DIRECTORY, imagePath); - const destinationPath = path.join(DESTINATION_DIRECTORY, imagePath); - const fileExtension = imagePath.split(".").pop() ?? ""; - const encoder = GET_ENCODER_FROM_EXTENSION[fileExtension]; + for (let i = 0; i < imagePaths.length; i += batchSize) { + await Promise.all( + imagePaths.slice(i, i + batchSize).map(async (imagePath) => { + const imageStartTime = Date.now(); - if (!encoder) { - await fse.copy(sourcePath, destinationPath); - console.log( - `Copied ${imagePath} in ${getElapsedSeconds(imageStartTime)}s` - ); - return; - } + const sourcePath = path.join(SOURCE_DIRECTORY, imagePath); + const destinationPath = path.join(DESTINATION_DIRECTORY, imagePath); + const fileExtension = imagePath.split(".").pop() ?? ""; + const encoder = GET_ENCODER_FROM_EXTENSION[fileExtension]; - const rawImageFile = await fse.readFile(sourcePath); - const ingestedImage = imagePool.ingestImage(rawImageFile); - const { width, height } = getImageDimensions(rawImageFile); + if (!encoder) { + await fse.copy(sourcePath, destinationPath); + console.log( + `Copied ${imagePath} in ${getElapsedSeconds(imageStartTime)}s` + ); + return; + } - await ingestedImage.decoded; + const rawImageFile = await fse.readFile(sourcePath); + const ingestedImage = imagePool.ingestImage(rawImageFile); + const { width, height } = getImageDimensions(rawImageFile); - const shouldResize = - (imagePath.startsWith(TEAM_IMAGES_DIRECTORY) || - imagePath.startsWith(EVENTS_IMAGES_DIRECTORY)) && - (width ?? 0) > IMAGE_MINIMUM_SIZE && - (height ?? 0) > IMAGE_MINIMUM_SIZE; + await ingestedImage.decoded; - if (width && height && shouldResize) { - const smallerDimension = width < height ? "width" : "height"; + const shouldResize = + (imagePath.startsWith(TEAM_IMAGES_DIRECTORY) || + imagePath.startsWith(EVENTS_IMAGES_DIRECTORY)) && + (width ?? 0) > IMAGE_MINIMUM_SIZE && + (height ?? 0) > IMAGE_MINIMUM_SIZE; - // specifying only one dimension maintains the aspect ratio - const preprocessOptions = { - resize: { - enabled: true, - [smallerDimension]: IMAGE_MINIMUM_SIZE, - }, - }; + if (width && height && shouldResize) { + const smallerDimension = width < height ? "width" : "height"; - await ingestedImage.preprocess(preprocessOptions); + // specifying only one dimension maintains the aspect ratio + const preprocessOptions = { + resize: { + enabled: true, + [smallerDimension]: IMAGE_MINIMUM_SIZE, + }, + }; + + await ingestedImage.preprocess(preprocessOptions); + + console.log( + `Resized ${sourcePath} in ${getElapsedSeconds(imageStartTime)}s` + ); + } + + const encodeOptions = { [encoder]: ENCODER_OPTIONS[encoder] }; + await ingestedImage.encode(encodeOptions); + + const encodedImage = await ingestedImage.encodedWith[encoder]; + await fse.outputFile(destinationPath, encodedImage.binary); console.log( - `Resized ${sourcePath} in ${getElapsedSeconds(imageStartTime)}s` + `Optimized ${sourcePath} in ${getElapsedSeconds(imageStartTime)}s` ); - } - - const encodeOptions = { [encoder]: ENCODER_OPTIONS[encoder] }; - await ingestedImage.encode(encodeOptions); - - const encodedImage = await ingestedImage.encodedWith[encoder]; - await fse.outputFile(destinationPath, encodedImage.binary); - - console.log( - `Optimized ${sourcePath} in ${getElapsedSeconds(imageStartTime)}s` - ); - }) - ); + }) + ); + } await imagePool.close();