soldair / node-walkdir

Walk a directory tree emitting events based on the contents. API compatable with node-findit. Walk a tree of any depth. Fast! Handles permission errors. Stoppable. windows support. Pull requests are awesome. watchers are appreciated.
MIT License
131 stars 22 forks source link

order of files is nondeterministic #17

Closed bwin closed 9 years ago

bwin commented 9 years ago

The order of the files emitted isn't constant. Is there a way to have a fixed order (=sorting)?

soldair commented 9 years ago

its asynchronous so the only way to make it deterministic is to wait for the end event and put the file paths into a sorted list.

something like this should work.


function deterministic(dir,cb){
  var bs = require('binarysearch');
  var walkdir = require('walkdir');
  var sorted = [];

  var emitter = walkdir(dir);
  walkdir.on('path',function(p){
    bs.insert(sorted,p);
  }).on('end',function(){
     cb(false,sorted);
  }).on('error',function(err){
    cb(err);
  });
}
soldair commented 9 years ago

perhaps this can be the default for the callback mode

bwin commented 9 years ago

Misread your comment and didn't look at the code (was on mobile). Described your approach wrong in my comment (see referenced above). Corrected now. Sorry.

soldair commented 9 years ago

so. do you think i should make the files array callback with a sorted list? i guess you aren't using it anymore but ill close this unless you have anything to add

bwin commented 9 years ago

In my latest code I use it. I tested

The last one slightly outperformed the binarysearch-version, so I chose the simple solution. There is virtually no performance hit in my test cases.

Thanks for participating.

so. do you think i should make the files array callback with a sorted list?

No. Because anyone can easily do result.sort().

soldair commented 9 years ago

perfect

bwin commented 9 years ago

Forgot to mention, I also tested

BTW the sort version was also faster than walkdir.sync in my test cases. Glob was the slowest.