TypeScript: High CPU when using --watch on 2000+ files (roughly 40megs)
Code
tsc -w
TypeScript version: 2.6.1 (reproduces in previous versions as well)
Expected behavior: To not hog CPU when watching (not while compiling).
Actual behavior:
tsc node process occupies 30-40% CPU while idle in watch.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 5
- Comments: 15 (7 by maintainers)
TSC_NONPOLLING_WATCHER=1 solves all my problems! @OlekNg thank you so much for this tip! CPU went from 15-40% to 0.5%. I can definitely live with that.
I’m also experiencing this issue. Machine i7 3632qm 12GB ram, SSD.
Project has almost 2000 ts files, and compiling in watch mode uses about 6-7% CPU constantly when waiting on changes: 18:59:26 - Compilation complete. Watching for file changes. Unfortunately I cannot share project source, because it’s closed software of my company.
I’ve seen similar issue when using grunt-watch task, which uses pooling to detect file changes. It’s good for small projects but it’s starting to stress cpu just watching hundreds of files. We switched to our custom implementation of watch using node fs.watch, which uses “native” host os watching for changes feature which is much more efficient with cpu than pooling (and much faster). Isn’t that the case? I couldn’t find info about what typescipt compiler uses internally for watching changes.
// Edit Actually I started to investigating this issue and confirmed it is related to pooling watcher mode. I found that you can set TSC_NONPOLLING_WATCHER env var to 1, and then tsc -w uses non pooling watcher. But there is another issue - we cannot set environment variables using built int tsc --watch build action in VSCode.