Lately, I've found myself worrying more and more about performance and the amount of time it takes for a function or a task to be taken on Node.js. Particularly, I wanted to benchmark and compare a particular code change to the original one. While researching for ways to implement and compare these functions, I came across Node.js's perf_hooks package which was added in the 8.5.0 release. For API definitions and use cases please refer to the documentation.
Let's first dive into a basic example of calculating the amount of time in milliseconds it takes to execute a specific function:
The important part you need to address in this code is, you need to manually mark a specific point in your codebase before and after the execution of it. After that, you need to measure the difference using performance.measure which takes the name/identifier of the measurement as the first parameter.
In order to log these measurements, you'll need the PerformanceObserver implementation from the perf_hooks.
In order to benchmark N number of implementations, you need to give the same input to both of them and compare them using the amount of time it takes to finish that task. For Node.js there's a really cool library called `benchmark` and provides a comparison and shows us the fastest implementation of our input.
Example implementation is:
This will give you a brief result of your comparison like: