Find Event Loop Pauses
In this How To section we'll discuss pauses in the event loop and how to detect them and what to do about them.
The Event Loop in Node.js is powered by a library called libuv that manages an IO queue and context for Node.js in order to parallelize IO operations while keeping the JavaScript execution on the main thread out of a 'wait' state.
Given that when not using Worker Threads Node.js is single-threaded, if you stay in JS too long, IO operations will not be able to resume and end up waiting anyway.
There are a few strategies for dealing with these large chunks of synchronous calculations that may block IO operations for too long at once:
- Improve performance.
- Break up portions to allow work insertion.
- Offload work to other services
- Add more workers
- Consider threads
Adding monitoring to the Event Loop to look for pauses.
The N|Solid runtime provides an event that specifies when your main event loop is being paused due to JS execution.
The N|Solid console allows to pin point when and where exactly your main loop was blocked/released. It is a simple as visiting the events view by clicking in the hamburguer icon and select events.
Then in the filter bar, select "Type: performance"
Too see the blocking stack trace, simply hover on "here" and you'll see what needs to be optimized or offloaded.