Typorithm · hard

Worker Threads

node-worker-threadsnodejsworker-threadscpu-boundparallelism
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Worker Threads

0%

Concurrency

Worker Threads run JavaScript on a separate thread for CPU-bound work. new Worker(path) spawns it, postMessage / parentPort exchange data, workerData seeds the worker at startup, and SharedArrayBuffer lets threads share memory. They are not for I/O — the event loop already handles that.

new Worker("./worker.js") loads a file and runs its JavaScript on a separate OS thread, giving you real parallelism for CPU-bound work like parsing, hashing, or image processing without freezing the main thread.

The two threads talk by message passing: the parent calls worker.postMessage and listens with worker.on("message"), while the worker uses parentPort to receive and reply. workerData carries an initial value provided at spawn time, and a SharedArrayBuffer can share raw memory directly when copying is too slow.

Worker Threads are not for I/O. Reading files or making network calls is already non-blocking via the event loop, so wrapping I/O in a worker adds overhead for no gain — reserve workers for genuinely CPU-bound computation.

01

new Worker(path) runs JS on a separate thread for CPU work.

02

postMessage / parentPort exchange data; workerData seeds init.

03

Not for I/O — the event loop already handles that.

import { Worker } from "node:worker_threads";

const worker = new Worker("./worker.js", { workerData: { rows: 1000 } });
worker.on("message", (result) => console.log("done", result));
worker.postMessage({ start: true });

// worker.js
import { parentPort, workerData } from "node:worker_threads";
parentPort.on("message", () => {
  const sum = heavyCompute(workerData.rows);
  parentPort.postMessage(sum);
});

Read the implementation

Connect each part of the code to the concept before you type.

1. Spawn a worker

new Worker(path) loads the file and runs its JS on a separate OS thread, in parallel with the main thread.

const worker = new Worker("./worker.js", { workerData: { rows: 1000 } });

2. Receive results

The main thread listens for messages the worker sends back, picking up the computed result asynchronously.

worker.on("message", (result) => console.log("done", result));

3. Send a message

postMessage sends data to the worker; values are structured-cloned, so the two threads do not share the object.

worker.postMessage({ start: true });

4. parentPort & workerData

Inside the worker, parentPort is the channel back to the parent and workerData holds the init value passed at spawn time.

import { parentPort, workerData } from "node:worker_threads";

5. CPU-bound work

Heavy synchronous computation runs here without blocking the main thread's event loop.

const sum = heavyCompute(workerData.rows);

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Worker Threads: Lesson & Practice · Typorithm | ApplyDjinn