Luke Oliff.

TIL: Parallel Processing in Node.js With worker_threads

·TIL·1 min read·Luke Oliff

Node.js runs JavaScript in a single thread by default. worker_threads gives you true parallelism for CPU-heavy work.

const { Worker } = require(worker_threads)

function runWorker(filepath) {
  return new Promise((resolve, reject) => {
    const worker = new Worker(./process-audio.js, {
      workerData: { filepath }
    })
    worker.on(message, resolve)
    worker.on(error, reject)
    worker.on(exit, (code) => {
      if (code !== 0) reject(new Error(`Worker stopped with exit code ${code}`))
    })
  })
}

const result = await runWorker(large-audio.wav)

The worker runs in its own thread with its own V8 instance. It can use 100% CPU without blocking the main thread.

// process-audio.js
const { workerData, parentPort } = require(worker_threads)
// CPU intensive work here
parentPort.postMessage({ status: done })

The worker receives data via workerData and sends results back with parentPort.postMessage.

Are workers expensive?

Each worker has startup overhead. Reuse them with a pool for batch processing instead of creating one per file.

Does this work with ES modules?

Yes. Use import instead of require and specify { eval: true } or point to an ESM file.