Writing TypeScript on a laptop? This might improve your battery life.

Writing TypeScript on a laptop? This might improve your battery life.

Two things happened recently:

  • My laptop’s battery was draining out too fast.

  • I began learning TypeScript.

I’m using tsc (TypeScript’s compiler) in watch mode which is very convenient to trigger automatic transpilation while developing.

But here’s what happens:

Node (which the TypeScript compiler runs on) is eating 8% of CPU cycles just while sitting down waiting for file changes. WOW.

A quick digging around pointed me to this link: https://github.com/Microsoft/TypeScript/pull/8196/

Apparently tsc reverted to a polling approach to watch for file changes which appears to be quite resource intensive. They made this choice because the non-polling approach used previously turned out to be slow at detecting file changes in many cases.

But not all hope is lost!

Luckily the non-polling file watcher is still in the code and can be enabled by setting this environment variable: TSC_NONPOLLING_WATCHER="1" .

So now watch this:

Now it is like it should be: No file changes, no CPU usage.

Up until now I did not notice any laziness at detecting file changes on a 2016 MacBook Pro 13" running mac OS 10.12.2 and the battery life got a big improvement since then.

Hope this helps.

Marco