Node.js's sweet spot has traditionally been with I/O-intensive processes. It's event-driven architecture allows I/O operations to run concurrently while their associated callbacks are executed synchronously in the event loop. This, however, comes with a dark side in that CPU-bound operations are likely to block the event loop thus delaying when those callbacks are invoked. Fortunately, Node.js ships with several modules that allow us to offload those blocking CPU-bound operations from the main event loop so it's free to handle other tasks while the CPU-bound operations go about their business. Most notably we'll explore some ways in which the child_process
and worker_threads
modules can keep your Node.js application performant while still handling those CPU-intensive operations.
- Clone the repository
- Install packages
npm i
In VS Code
- Open the folder in Visual Studio Code
- Run the project
- Navigate to http://localhost
From the CLI
- Navigate to the folder containing the demo
- Run
node app.js
- Navigate to http://localhost