From 57fee8589a8e8cb1b6316202b72c1378264b0cc7 Mon Sep 17 00:00:00 2001 From: Arjun Aditya Date: Thu, 12 Sep 2024 11:38:42 +0530 Subject: [PATCH] optimization --- index.js | 32 ++++++++++++++++++++++++-------- optimized.json | 3 +++ 2 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 optimized.json diff --git a/index.js b/index.js index 92fa639..07c100c 100644 --- a/index.js +++ b/index.js @@ -3,25 +3,41 @@ const fs = require('fs').promises; const path = require('path'); const svgDirPath = './covers/svg'; +const outputJsonPath = './optimized.json'; -console.time('Optimization Time'); +const startTime = Date.now(); fs.readdir(svgDirPath) .then(files => { const svgFiles = files.filter(file => path.extname(file) === '.svg'); - return Promise.all(svgFiles.map(async file => { + const processingPromises = svgFiles.map(file => { const svgFilePath = path.join(svgDirPath, file); - const svgData = await fs.readFile(svgFilePath, 'utf-8'); - const result = optimize(svgData, { path: svgFilePath }); + return fs.readFile(svgFilePath, 'utf-8') + .then(svgData => { + const result = optimize(svgData, { path: svgFilePath }); + return fs.writeFile(svgFilePath, result.data) + .then(() => { + console.log(`Optimized: ${file}`); + }); + }); + }); - await fs.writeFile(svgFilePath, result.data); - console.log(`Optimized: ${file}`); - })); + return Promise.all(processingPromises); }) .then(() => { - console.timeEnd('Optimization Time'); + const endTime = Date.now(); + const timeTaken = endTime - startTime; + + const resultJson = { + timeTaken: `${timeTaken} ms` + }; + + return fs.writeFile(outputJsonPath, JSON.stringify(resultJson, null, 2)) + .then(() => { + console.log(`Time taken: ${timeTaken} ms`); + }); }) .catch(err => { console.error('Error:', err); diff --git a/optimized.json b/optimized.json new file mode 100644 index 0000000..1c633e5 --- /dev/null +++ b/optimized.json @@ -0,0 +1,3 @@ +{ + "timeTaken": "566 ms" +} \ No newline at end of file