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 (fs.statSync(filepath).isDirectory()) {
            walk(filepath, filepaths);
        } else if (path.extname(filename) === '.md') {
    return filepaths;

And you use it like this:

const foundFiles = walk(someDirectoryOfMine);

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:

const foundFiles = walk(someDirectoryOfMine);

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


Your email will never ever be published

Related posts

Train your own spell corrector with TextBlob 23 August 2019
Fastest Python function to slugify a string 12 September 2019
Related by Keyword:
How to install Node 12 on Ubuntu (Eoan Ermine) 19.10 08 April 2020
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