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
130 stars 22 forks source link

Make a few performance tweaks #6

Closed gjtorikian closed 10 years ago

gjtorikian commented 11 years ago

I wanted a directory walking library that was very performant. Fortunately, I found this one !

I kept making a few tweaks along the way, because I wanted to get the search faster. I learned a lot about Node profiling and Javascript optimizations. Ultimately, I decided to gut your library and take just the parts I needed. I'm contributing back a few pieces of code that dropped my search speed down by a few hundred milliseconds.

Two other tweaks could make this library even faster:

  1. Drop the event emitting when in synchronous mode. This includes doing stuff like:
emitter.emit('path', path, stat,depth)
...
if (options.sync) {
    if(!options.no_return){
      emitter.on('path',function(path,stat){
        if(options.return_object) allPaths[path] = stat;
        else allPaths.push(path);
      });
    }
  }

A simple function call is more perfomant. Likewise, not doing the emits when in synch also makes it performant.

  1. Instead of allResults.push(), doing allResults += path + "\n" is much better at providing a list of results. Possibly because push always takes n amount of time?

I realize both of these pertain to improving the sync speed, but that's because I was concerned with an organized directory search.

soldair commented 11 years ago

hey. im soooo sorry for not noticing your pull request.

thanks for the feedback. yeah sync support is not necessarily something you normally need in node server programs but ill take any help i can get making all parts of this faster. I'm interested in what you ended up with i didnt see anything on github. perhaps there is a way to get those improvements without loosing features so i can give your boost to everyone.

gjtorikian commented 11 years ago

No worries!

My code can be found at http://WWW.GitHub.com/c9/nak . In the end, I did go with the async format, and just perform a mergesort at the very end of the walk.

I'm pretty sure I've gotten it as quick as can be. I'm sorry to say that I learned EventEmitters are not quick, so perhaps you could choose to emit events on an optional configuration.

soldair commented 11 years ago

i know event emitter 2 is much faster. thanks alot for the link

On Tue, Nov 20, 2012 at 4:00 PM, Garen Torikian notifications@github.comwrote:

No worries!

My code can be found at http://WWW.GitHub.com/c9/nak . In the end, I did go with the async format, and just perform a mergesort at the very end of the walk.

I'm pretty sure I've gotten it as quick as can be. I'm sorry to say that I learned EventEmitters are not quick, so perhaps you could choose to emit events on an optional configuration.

— Reply to this email directly or view it on GitHubhttps://github.com/soldair/node-walkdir/pull/6#issuecomment-10580059.