NodeJS fs walk() or glob or fast-glob

31 August 2019   0 comments   Javascript

It started with this:

function walk(directory, filepaths = []) {
    const files = fs.readdirSync(directory);
    for (let filename of files) {
        const filepath = path.join(directory, filename);
        if (path.extname(filename) === '.md') {
            filepaths.push(filepath);
        } else if (fs.statSync(filepath).isDirectory()) {
            walk(filepath, filepaths);
        }
    }
    return filepaths;
}

And you use it like this:

const foundFiles = walk(someDirectoryOfMine);
console.log(foundFiles.length);

I thought, perhaps it's faster or better to use glob. So I installed that.
Then I found, fast-glob which sounds faster. You use both in a synchronous way.

I have a directory with about 450 files, of which 320 of them are .md files. Let's compare:

walk: 10.212ms
glob: 37.492ms
fg: 14.200ms

I measured it using console.time like this:

console.time('walk');
const foundFiles = walk(someDirectoryOfMine);
console.timeEnd('walk');
console.log(foundFiles.length);

I suppose those packages have other fancier features but, I guess this just goes to show, keep it simple.

Comments

Your email will never ever be published

Related posts

Previous:
Train your own spell corrector with TextBlob 23 August 2019
Next:
Fastest Python function to slugify a string 12 September 2019
Related by Keyword:
function expandFiles(directoriesPatternsOrFiles) 15 August 2019
How's My WiFi? 08 December 2017
Python slow-down of exception handling or condition checking 14 May 2015
How I stopped worrying about IO blocking Tornado 18 September 2012
Slides about Kwissle from yesterdays London Python Dojo 08 July 2011