What happens when you can no longer trust your file watcher
Tags: node, opensource, javascript, npm
Published: 2019-10-01 01:35:41

With the [fanfare surrounding the release](https://paulmillr.com/posts/chokidar-3-save-32tb-of-traffic/) of [chokidar v3.0.0](https://github.com/paulmillr/chokidar), I created an innocuous task for myself to [update one of my modules to use the brand new versions](https://github.com/catdad/electronmon/issues/25). And little did I know, this is where a new adventure began.

The update went just fine, as chokidar v3.0.0 is pretty much a drop-in replacement. However, my macOS build started failing. Chalking it up to a sub-optimal design of my module, I began what turned out to be a complete rewrite, far more streamlined than the first version. However, at no point did the macOS build start passing. Curious, I began to investigate and ran across a bug. While it seems chokidar is perfectly happy to watch file when left on its own, [as soon as you start doing other work in your application, it no longer fired the ready event](https://github.com/paulmillr/chokidar/issues/873). A [Windows crashing bug](https://github.com/paulmillr/chokidar/issues/871) and a [Linux CPU bug](https://github.com/paulmillr/chokidar/issues/860) solidified the issues for me.

Seeing as how watching files is only a feature in my otherwise not file-watching-centric application, I would like the luxury of doing other work while files are being watched in the background. I had to trust that my file watcher has my back, even if I decide to use a few CPU cycles on something else. So like any good engineer, I started wondering "🤔 what goes into watching files?"

# Why is watching files hard?

We've all heard it before... watching files is hard and we should lean on the battle-tested libraries to handle it for us. But have you wondered where this story began? It turns out the answer is 2012. Node was still at version 0.6 (version 0.8 to be released later that year), the community was a wild west, and anyone using it was a rebel. Those were the days. (For those wondering, gaze got started that same year as well.)

With such a young ecosystem came various shortcomings. In the world of file watching, support was rather patchy and inconsistent. It didn't report filenames on MacOS (née OSX), events were sometimes (often) reported twice, only a non-useful `rename` event is present, and recursive watching wasn't really a thing. And that all came from the [README from 2012](https://www.npmjs.com/package/chokidar/v/0.1.1). The story [hasn't changed much today](https://www.npmjs.com/package/chokidar/v/3.1.1), but you would be wrong in assuming that the implementation has not changed either. Since then, Node has started relying on `FSEvents` for watching files on MacOS, started reporting filenames, added recursive watching on Windows, and gotten many other improvements. Seems that a mere mortal developer like me could even get something done.

# Watching files, revisited

> Here we go again... someone is trying to reinvent the wheel, or... I guess... watching the wheel?

Hey now! I thought we liked each other. I wouldn't waste your time on just another version of the same old thing. I am talking about [watchboy](https://github.com/catdad/watchboy), and he's magnificent!

You see, with 7 years of fixed issues and gained knowledge, we can get some good things done. If a library is going to earn its spot in the ring, it doesn't only need to solve the problem at hand (that is, watching files), but it needs to do it faster, do it with less memory usage, be smaller, be simpler to use, and be backed by more straightforward and easy to understand code. Watchboy is all that and more. That's right, it also shows you a picture of a dog!

# Show me the numbers

Watchboy wasn't just built to watch files. It was built for speed, built for low memory usage, built for code simplicity, all right from the very first line of code. Those are all things that are difficult to retrofit after years of only "solving the problem". So let's look at some numbers.

In this first chart, we'll take a look at watching the source code of a pretty small library, [React](https://github.com/facebook/react/tree/a5df18a9e5d7cc70c30ce144dcc291e9f64cb451).

![react chart](https://thepracticaldev.s3.amazonaws.com/i/5ycs9zdnvx12xp9zlzn5.png)

Before we jump to conclusions, let's look at watching a larger repository of source code, [VSCode](https://github.com/microsoft/vscode/tree/6de911e3298424cfb6dd6792bff5499f1cd6a230) itself (which ironically uses chokidar for watching files).

![vscode chart](https://thepracticaldev.s3.amazonaws.com/i/mvi5mf30cy1ir95490mn.png)

Hmm... this image looks familiar. But let's do one more before we talk about it. Let's look at the source code of a completely diabolical project, [Babel](https://github.com/babel/babel/tree/a79fbe62e4bfdf0434466ff70e57ed037399b608). It comes in at 16,847 files in 8,276 directories.

![babel chart](https://thepracticaldev.s3.amazonaws.com/i/lggpz9eypuzhrpjs89rg.png)

# Discussion

Okay, let's get the elephant in the room out of the way. MacOS is so fast! That's gotta be the winner of this benchmark, right? Right? Leave me a comment about this if you want... but be nice, I am sensitive.

Now some real talk: we can see an issue pretty quickly. Gaze does okay on Linux, and chokidar does well on MacOS, but Windows is basically a neglected case. The [operating system used by half of developers in 2019 is a second rate citizen](https://insights.stackoverflow.com/survey/2019#technology-_-developers-primary-operating-systems) (with consumer numbers even higher), up to 5x slower than MacOS, with Linux not holding up much better. Now, based on the results before looking at watchboy, it's easy to think "Windows just sucks... I've even heard people saying that before". But the fact of the matter is that you can't just neglect it. For that matter, you can't neglect Linux either, [considering that it has just as many developers using it as MacOS](https://insights.stackoverflow.com/survey/2019#technology-_-developers-primary-operating-systems). When you build from the ground-up for speed and consistency, you can make your software behave the same on all operating systems. Looking at the watchboy results, we see far more consistency across all operating systems in time to watch files, with consistently low memory usage.

You may have also noticed that gaze was left out of the last chart. That is because watching that many files results in `Maximum call stack size exceeded` error.

# Show me the code

I must have rigged these results, right? I mean, I am biased after all. I welcome you to take a look at [the code and the numbers](https://github.com/catdad-experiments/filewatch-benchmarks) more closely, and to add your own submissions to the benchmark. The hardware for these tests is standardized to the CI environment, so we can all reproduce the same results.

If you'd like to help make [watchboy](https://github.com/catdad/watchboy) even better, send a PR my way!