Optimizing synchronous functions
The previous recipes of this chapter covered how to detect hot code paths in our applications. Once a hot code path is identified, we can focus our optimization efforts on it to reduce the bottleneck.
It’s important to optimize any hot code paths as any function that takes a long time to process can prevent I/O and other functions from executing, impacting the overall performance of your application.
This recipe will cover how to micro-benchmark and optimize a synchronous function. A micro-benchmark is a type of performance test that focuses on a small, specific piece of code or functionality within a larger system. We’ll use Benchmark.js (https://benchmarkjs.com/) to create a micro-benchmark.
Getting ready
In real applications, we’d use tooling such as flame graphs or profilers to identify slow functions in our applications. For this recipe, we’ll create a single slow function that we can learn how to micro-benchmark...