Skip to content

Commit

Permalink
optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
nermalcat69 committed Sep 12, 2024
1 parent 60e3817 commit 57fee85
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
32 changes: 24 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions optimized.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"timeTaken": "566 ms"
}

0 comments on commit 57fee85

Please sign in to comment.