[go: up one dir, main page]

DEV Community

Cover image for Seamless workers for Node.js
Christophe Marois
Christophe Marois

Posted on

Seamless workers for Node.js

Node.js supports native Worker Threads without flags since 12.0, and this finally allows us to circumvent the limitations associated with the single-threaded nature of JavaScript.

Workers in Node can only communicate via a RPC pattern (same as their web counterparts), which is not a bad thing at all: it enables concurrency, like the go coroutine pattern used in golang.

However, sometimes you just want a drop-in way to execute a function without blocking the main thread (for instance, regenerating a server's cached data without blocking incoming requests to the previous cached data).

For WebWorkers, Workerize has been providing exactly this. You can execute a function normally without having to change its signature, but it runs in a worker under-the-hood.

I created the node-inline-worker npm package (Github) to achieve the same thing, leveraging the Worker Thread of Node.js instead of the WebWorker API, while keeping the same interface API as Workerize. With it, making a function truly non-blocking is as simple as changing:

const result = await expensiveFn(1, 2)

to this:

const result = await require('node-inline-worker')(expensiveFn)(1, 2)

Note: Worker Threads will not magically solve your performance issues, and have some downfalls. For instance, Node.js documentation's states that:

Workers (threads) are useful for performing CPU-intensive JavaScript operations. They will not help much with I/O-intensive work. Node.js’s built-in asynchronous I/O operations are more efficient than Workers can be.

I suggest that you read the Worker Thread documentation page (especially the Notable differences inside a Worker environment section) to find out if workers are suitable for your use case.

For more information on how to use node-inline-worker, go read the very straightforward example on the Github repo's page.

Have a good day!

Top comments (0)