Question

I'm attempting to write a bunch of async fs calls in bluebird promises. It all works great except that I'm worried about performance. I can't tell if the .map and .filter functions are executing in parallel or sequentially. Here's my code:

var Promise = require('bluebird'),
  fs = Promise.promisifyAll(require('fs'));

module.exports = getDirListing;

function getDirListing(path) {
  function toFullPath(file) {
    return path + file;
  }
  function onlyDirs(file) {
    return fs.statAsync(file).then(function(file) {
      return !file.isFile();
    });
  }
  function ignoreHidden(name) {
    return !(name.substring(0,1) === '.');
  }
  function toModuleNames(dir) {
    return dir.replace(path, '');
  }
  return fs.readdirAsync(path)
    .map(toFullPath)
    .filter(onlyDirs)
    .map(toModuleNames)
    .filter(ignoreHidden);
}

I especially care about the call to onlyDirs since that is making a bunch of async fs.stat calls and can/should be in parallel. I tried looking at their api docs but I didn't see anything about parallelization.

Note: I'm not currently in a position to use ECMAScript6 generators (unfortunately).

Also, any other pointers on performance here are welcome.

Was it helpful?

Solution

The fs.stat calls are done concurrently which you can verify with:

var id = 0;
function onlyDirs(file) {
  var d = id++;
  return fs.statAsync(file).then(function(file) {
    console.log("returned from stat call %d", d);
    return !file.isFile();
  });
}
returned from stat call 0
returned from stat call 1
returned from stat call 2
returned from stat call 3
returned from stat call 5
returned from stat call 4
returned from stat call 6
returned from stat call 7
returned from stat call 8
returned from stat call 10
returned from stat call 11
returned from stat call 14
returned from stat call 13
returned from stat call 9
returned from stat call 15
returned from stat call 18
returned from stat call 17
returned from stat call 16
returned from stat call 12

Note the code didn't work originally, I had to modify this:

var Path = require("path");
function toFullPath(file) {
    return Path.join(path, file);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top