-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FSWatcher.close() incurs a big performance cost in >=10.16 #29949
Comments
Hello, I noticed too that closing a lot of const fs = require('fs');
const watchers = [];
for (let n = 0; n < 1200; n += 1) {
watchers.push(fs.watch(process.cwd()));
}
console.log('closing watchers...');
console.time('closed');
for (let w of watchers) {
w.close();
}
console.timeEnd('closed'); On my laptop it's quite slow, here the output:
|
Noticing a similar issue on macOS Monterey 12.4, intel, node: v16.16.0. Closing a watcher takes up to 50ms, so closing 1k+ in a tight loop leads to the process being busy for many seconds. Reproducing is trivial: create 5k watchers and then time the closes: const fs = require("fs");
const { performance } = require("perf_hooks");
const filenames = [/* 3k+ paths to files/directories */]
filenames
.map((filename) => fs.watch(filename))
.forEach((watcher) => {
const start = performance.now();
watcher.close();
const duration = performance.now() - start;
console.log(duration);
}) From the output of this it's pretty clear what's happening: the first durations printed are ~35ms and they gradually decrease to sub-ms times, so Encountered this issue inside watchpack which "intelligently" replaces non-recursive watchers with recursive watchers when the total number of watchers hits some limit. When this replacement happens, watchpack will close the already-opened watchers, and if there are already a lot of files being watched this can take a long time. |
There is a node.js bug on MacOS which causes closing file watchers to be really slow. nodejs/node#29949
### What? This adds a workaround for a Node.js bug on MacOS which causes closing file watchers to be really slow: nodejs/node#29949 It was fixed before here: #51826 But accidentically removed here: c5a8e09#diff-0ff576bd02e76f96680accff48ecbe4126a7f6bc4eafa547b6d44dee49bab770L88
There seems to have been a massive performance decrease in FSWatcher.close() introduced in 10.16.
I use
ts-node-dev
as a file watcher when developing web applications, which under the hood usesfilewatcher
.I believe the following commits/PRs could be contributing? Not smart enough to tell you why though 😅
Some examples w/ a pretty small express app:
10.15
save some files...
10.16
save some files...
Method
Perf code added to the following: https://github.com/fgnass/filewatcher/blob/master/index.js#L130
One might be tempted to think the
list()
method could be slow, however adding code right above it, a single invocation of watcher.close() took longer than all of them in 10.15.The text was updated successfully, but these errors were encountered: