BioDrop
BioDrop copied to clipboard
"Use of async/await and fs.promises to Improve efficiency of profile loading code"
Description
Using the 'async' and 'await' keywords, as well as the 'fs.promises' API, can improve the efficiency of the code by allowing it to perform multiple operations concurrently, rather than waiting for each operation to finish before starting the next one. This can result in faster execution times and reduced server load.
Additionally, using pagination or filtering to limit the number of documents retrieved from the 'Profile' collection can also improve efficiency by reducing the amount of data that needs to be processed.
Finally, wrapping the 'JSON.parse' function in a try-catch block can help handle any errors that might occur during parsing, which can help improve the reliability and robustness of the code.
Screenshots
Additional information
I hope this helps! Let me know if you have think there should be any changes required.
It's great having you contribute to this project
Welcome to the community :nerd_face:If you would like to continue contributing to open source and would like to do it with an awesome inclusive community, you should join our Discord chat and our GitHub Organisation - we help and encourage each other to contribute to open source little and often 🤓 . Any questions let us know.
Challenge with doing them async, is that we can't append the username from the file name into the json
const filesToLoad = [];
files.forEach((file) => {
const filePath = path.join(directoryPath, file);
try {
filesToLoad.push(fs.promises.readFile(filePath, { encoding: "utf8" }));
} catch (e) {
console.log(`ERROR loading profile "${filePath}"`, e);
}
});
const loadedFiles = await Promise.all(filesToLoad);
const users = loadedFiles.flatMap((profile) => {
try {
return {
...JSON.parse(profile),
// username: file.split(".")[0],
};
} catch (e) {
console.log(`ERROR parsing profile "${profile.name}"`);
return [];
}
});
After some thought I will close this as loading all profiles only effects the search page and we can solve this another way instead by only searching when the user has entered some characters