Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Module

worker_threads

Node.js worker_threads runs JavaScript in parallel worker threads, primarily for CPU-intensive tasks. Workers communicate through message ports and can transfer or share memory.

Since
v10.5.0

How it works

The module provides independent JavaScript execution threads. A parent thread starts a Worker, and the threads communicate through message ports; ArrayBuffer objects can be transferred and SharedArrayBuffer objects can be shared.

Syntax

new Worker(filename[, options])

Parameters

NameTypeDescription
filenamestringPath to the worker's main script; it must be absolute or begin with ./ or ../ when relative.
optionsObjectOptional Worker configuration, including execution and resource settings.

Returns

A Worker instance representing the newly started execution thread.

Key points

  • Import node:worker_threads to run JavaScript work concurrently in separate threads.
  • Use workers to move CPU-bound JavaScript away from the main event-loop thread.
  • Worker instances encapsulate independently executing JavaScript threads.
  • A worker can use most Node.js APIs available to regular JavaScript execution.
  • Data can cross worker boundaries by transferring ArrayBuffer instances or sharing SharedArrayBuffer instances.
  • MessageChannel supplies an asynchronous bidirectional channel for passing messages between execution contexts.

Common mistakes

  • Use workers for CPU-heavy JavaScript rather than ordinary I/O, where Node.js asynchronous APIs are more efficient.
  • Workers are not process-isolated in the same way as child processes; shared memory requires deliberate coordination.
  • When passing a script filename to Worker, use an absolute path or an explicitly relative path.

Example: Basic worker communication

const { Worker, isMainThread, parentPort } = require('node:worker_threads');

if (isMainThread) {
  const worker = new Worker(__filename);
  worker.once('message', (result) => console.log(result));
  worker.once('error', console.error);
} else {
  parentPort.postMessage(6 * 7);
}

Creates a worker from the current file and exchanges one message with it.

When to use it

Parallel computation

Offload CPU-bound calculations so the event loop remains responsive.

Concurrent processing

Run independent JavaScript tasks concurrently within one Node.js process.

Thread communication

Exchange structured data or buffers between a parent and worker.

Interview questions

QWhen should worker_threads be used?

Worker threads run JavaScript in parallel in independent execution threads. They are intended mainly for CPU-intensive work, while asynchronous I/O is generally more efficient without workers.

Follow-up: How can worker and parent exchange data? Through message ports using postMessage and message events; ArrayBuffer instances may be transferred and SharedArrayBuffer instances shared.

QWhat does the Worker class represent?

The Worker class represents an independent JavaScript execution thread. Most Node.js APIs are available inside it, and the parent communicates with it through its message interface.

Follow-up: How does this differ from child_process? Workers can share memory by transferring ArrayBuffer instances or sharing SharedArrayBuffer instances.

QHow do worker threads communicate?

The module exposes communication primitives such as parentPort and MessageChannel. A worker uses parentPort to communicate with its spawning thread, while MessageChannel provides an asynchronous two-way channel.

Follow-up: What filename forms are accepted by Worker? The path must be absolute or relative to the current working directory beginning with ./ or ../.

  • Worker
  • MessageChannel
  • MessagePort
  • parentPort
  • isMainThread
  • workerData
  • SharedArrayBuffer

Source: Worker threads. Checked 2026-10-15.