I work with a program that deals with files, I can do several things, for example, rename them, read their contents, etc.
Today I initialize it as follows:
return new Promise((resolve, reject) => { glob("path/for/files/**/*", { nodir: true }, (error, files) => { files = files.map((file) => { // properties like full name, basename, extension, etc. }); resolve(files); }); });
So, I read the contents of a specific directory, returned all the files in the array, and then used Array.map to iterate through the array and change the paths for the object with properties.
Sometimes I work with 200,000 text files, so this becomes a problem because it consumes too much RAM.
So, I want to replace with a lazy loading construct .. but I never did this ... so I'm looking for help.
What is my code:
class File { constructor(path) { this.path = path; } extension() { return path.extname(this.path); }
So my main question is: should I only return the property rating, or should I replace it? Like this:
extension() { this.extension = path.extname(this.path); }
I understand that this is a compromise. I am going to exchange memory for processor use.
Thanks.
user5526811
source share