Sitemap
Press enter or click to view image in full size

Finding dead code in nodejs projects

3 min readSep 16, 2019

Over the last couple of months, I’ve been handed a project that needed cleanup. It’s a five year old JavaScript project of about 20kLoC. Code quality isn’t excellent, and there are tons of cruft.

So, to clean this thing up, we started writing tests so we could refactor without too much risk. Then, on one occasion, we spent two days writing tests for code that wasn’t called. The author hadn’t removed the old code, but the clients had all stopped using it. A slight depression followed.

We decided we needed to spend some time finding and removing dead code, preferably simply and efficiently. What we wanted was to identify dead code, so we could remove it.

Now, I know the V8 engine that powers Node has support for quite a bit of code instrumentation. After a bit of digging, I found the c8 project. c8 is what allows test runners like nyc to interface with V8.

What we wanted to do was to run this in production for some hours. If there is code that doesn’t get any usage it would be a good candidate for removal. While running, we noticed a slowdown of about 20-40% or so. Bear this in mind on busier systems.

The only issue I came into was how to stop the server when it is running with instrumentation. Turns out that sending ^c to a program running under c8 interfere with c8 and causes it to spit out empty reports. Delivering SIGTERM to either the c8 process or the underlying node process produces the same result. I installed a signal handler in my server process which conducts an orderly exit.

Example signal handler in Node. The server class is listening for events and the actual shutdown happens within the class.

To write this post I tried the same on a test-project I had lying around. It is just a API that implements a simple calculator. I would recommend downloading and playing around with it until you are comfortable using this on your software.

Get Per Buer’s stories in your inbox

Join Medium for free to get updates from this writer.

The code is installed and invoked like this:

$ npm install -g c8

$ c8 node server.js

After exiting you can then look at the output. “c8 report” will give you the report in your console. For something useful I would recommend creating the HTML report, like this:

$ c8 --reporter=html report

The output is is a report in the folder “coverage”. Open it your browser and you’ll see something like this. It is pretty self-explanatory.

Press enter or click to view image in full size

The output should be familiar if you’ve dealt with test-runners. Below is a typical example of code that isn’t invoked. Notice the green column that counts the number of invocations for that particular line.

Press enter or click to view image in full size

So, with this in your tool chest, you should be able to run your project under c8. The output will help you track down the dead code in your nodejs project.

Thanks for reading this. Good hunting.

Per Buer
Per Buer

Written by Per Buer

KnowIT. Previously: IncludeOS. Varnish.