gulp.watch High CPU Usage
I noticed frequent slowdowns on my new work PC with Windows 10, an i7 and 16GB of RAM, and opened up Task Manager to see what could possibly have been slowing everything down. To my surprise, I found that the node process was hogging between 25 and 50 percent of CPU while it was running. When developing our frontend application, I run an Express server and several gulp.watch
tasks to trigger a build whenever a .scss, .html or .js file in the project is changed. Here is ascreenshot of the process on the lower end of CPU utilization:
Since Express shouldn’t be taking up that many resources for our use case, I narrowed it down to gulp.watch
high CPU usage. I looked around and found this GitHub issue in which a user called shama suggested that the interval for the watch tasks should be set higher than the default of 100ms. I set mine to 750ms and the CPU usage dipped dramatically, from a constant 25-50% to a much lower 6-12% as demonstrated below:
The interval must be passed as the second argument to a watch task, and your gulp.watch high CPU usage woes should be banished!
gulp.watch('src/**/*.js', { interval: 750 }, function(event) {
// watch code here
}