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?
IntermediateAnswer
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 processReal-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.