Optimizing synchronous functions
The previous recipes of this chapter have covered how to detect hot code paths in our applications. Once a hot code path is identified, we can then 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. We'll use Benchmark.js (https://github.com/bestiejs/benchmark.js) 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 and optimize:
- First, create a directory for the recipe code and initialize the project:
$ mkdir optimize...