Search code examples
node.jsgulpgulp-watch

chokidar slows down every time watched files are changed


I have build setup using gulp and I use chokidar for listening for file changes:

var buildWatcher = chokidar.watch(paths, { ignoreInitial: true });

buildWatcher
    .on('add', function(filePath) {
        gulpUtil.log("File event: " + gulpUtil.colors.cyan(path.basename(filePath) + ' add'));
        gulp.start(key + suffix);
    })
    .on('change', function(filePath) {
        gulpUtil.log("File event: " + gulpUtil.colors.cyan(path.basename(filePath) + ' change'));
        gulp.start(key + suffix);
    })
    .on('unlink', function(filePath) {
        gulpUtil.log("File event: " + gulpUtil.colors.cyan(path.basename(filePath) + ' unlink'));
        gulp.start(key + suffix);
    });

I'm struggling with an issue, like a memory leak or sth, namely: every time a file is saved, the watch process is taking more resources (CPU in Task Manager) and as a result whole gulp.start'ed task takes longer. When starting new gulp, initially tasks are running smoothly, CPU usage is low, started tasks take a couple of ms to execute. But saving (even the same file) a few times, and CPU usage raises to 100% for a few seconds.

I tried switching to gulp.watch (3.9):

gulp.watch(paths, function(event) {
    gulpUtil.log("File event: " + gulpUtil.colors.cyan(path.basename(event.path) + ' ' + event.type));
    gulp.start(key + suffix);
}).on('error', function(error) {
    gulpUtil.log(error);
});

and here I don't have this issue. However gulp.watch does not support listening for new files, and this is why I wan't to use chokidar.

Do you know what could be the issue behind chokidar performance issue? How can it be diagnosed?

Thanks


Solution

  • I dont know about the performance of chokidar, but I will suggest you to use gulp-watch.

    Notice that you need absoulte path for that.

    Check also why gulp.watch doesnt support new/removed files here.