Skip to main content
CPU-bound JavaScript on up to 8 OS threads. Each Worker has its own JS runtime, event loop, and heap (32 MB) — no shared state, only messages.

Constructor

Throws TypeError: Worker requires a module path with no argument. options.data that cannot be cloned throws value could not be cloned.
The parameter is named “module path”, but the file is evaluated as a classic script (EVAL_TYPE_GLOBAL) — import / export inside a worker file is a SyntaxError. Wrap shared code by copying it, or load it in the main thread.

Parent side

Methods

Handlers

Both are single-valued properties — assigning twice replaces the first.

Worker side

Globals available inside the worker file: Not available: fetch, fs, http, Database, sql, process, crypto.bcrypt, import.

Message contract

Values are structured-cloned across the boundary: Cloning copies: the receiver gets a snapshot, not a reference. Mutations after postMessage do not propagate.

Limits

Pattern: worker pool

Eight is the ceiling — beyond that, queue in JS and keep 8 warm.

Lifecycle

  • A worker never exits on its own — even an empty script keeps its thread alive, so the main process waits. Call terminate().
  • terminate() is immediate; pending postMessages in flight are lost.
  • An uncaught throw, timer callback error, or microtask error in the worker immediately wakes the parent loop and fires onerror on the parent; the worker thread keeps running unless you terminate it.

Gotchas

  • Worker files are classic scripts — no import/export, no top-level await.
  • No shared memory: SharedArrayBuffer exists as a global but Workers don’t share one — messages are the contract.
  • structuredClone(value) uses the same binary serializer (max 4 MB) on both the main thread and inside Workers.
  • 4 MB messages are expensive — send indices, not payloads.
  • console in a worker writes to the same stderr — prefix it (console.log("[w" + workerData.id + "]")).

See also