Node.js › Clustering & Worker Threads

What is the difference between spawning a Worker Thread and spawning a Child Process for running a CPU-intensive JavaScript function?

Intermediate

Answer

A Worker Thread runs within the same process, sharing the V8 binary but with its own JS heap, communicating cheaply and able to share memory via SharedArrayBuffer. A Child Process is a fully separate OS process with isolated memory, higher creation overhead, but stronger isolation -- a crash can't corrupt the parent's memory.
const worker = new Worker('./cpu-task.js'); // lighter weight, shared process
const child = fork('./cpu-task.js'); // heavier, fully isolated process
Real-world example A sandbox running untrusted user-submitted JavaScript deliberately uses child_process.fork() with resource limits rather than a worker thread, since stronger process isolation prevents a malicious snippet from corrupting the parent's memory.

Common follow-ups: In what scenario is the stronger isolation of a child process worth the higher overhead?;How does IPC between a parent and forked child compare to postMessage() between worker threads?