Use Promise.all on batches of 32
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
4c66e9f8a5
commit
0360382882
|
@ -46,64 +46,69 @@ 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();
|
||||
// process smaller batches in order to reduce memory usage
|
||||
const batchSize = 32;
|
||||
|
||||
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();
|
||||
|
||||
|
|
Loading…
Reference in New Issue