How efficient is Chokidar (Node.js)?

2019-03-20 08:19发布

问题:

I have a caching engine on the server which caches all files accessed under a root directory. I'm thinking of using Chokidar to watch the entire directory tree (recursively) for file changes and update the cache accordingly. But I'm concerned about what would happen if a sub directory contained hundreds of thousands of files - How efficient would Chokidar be?

回答1:

Chokidar's efficiency depends on which operating system it's running on.

On OS X, it uses a module that provides access to the native fsevents API, which is extremely efficient.

On other systems, it uses node.js's fs.watch or fs.watchFile APIs. Under the hood, fs.watch uses various system APIs to be notified of changes, which may be reasonably efficient. fs.watchFile uses stat polling, which would definitely be inappropriate for directories as large as you're describing.

My suggestion is that you set the chokidar option usePolling: false and give it a try, monitoring your cpu load.

Update (July 2015): Chokidar has been substantially improved since this was originally written, and polling is no longer the default on any platform.



标签: node.js npm fs