Optimizing asynchronous functions
The Node.js runtime was built with I/O in mind, hence its asynchronous programming model. In the previous recipes of this chapter, we explored how to diagnose performance issues within synchronous JavaScript functions.
However, a performance bottleneck may occur as part of an asynchronous workflow. In this recipe, we’ll cover profiling and optimizing an asynchronous performance problem.
Getting ready
In this recipe, we’ll diagnose a bottleneck in an Express.js web server that communicates with a MongoDB database. For more information on MongoDB, please refer to the Storing and retrieving data with MongoDB recipe in Chapter 5:
- To start MongoDB, we’ll use Docker (as we did in Chapter 5). Ensuring that you have Docker running, enter the following command in your terminal to initialize a MongoDB database:
$ docker run --publish 27017:27017 --name node-mongo --detach mongo:7
- Now, we need to create a directory to work...