10LOC

#worker-threads

Bunadvanced

Parallel work with Bun's Worker

export const countPrimesOnWorker = (from: number, to: number) => {
  const workerCode = `
    self.onmessage = ({ data: [from, to] }) => {
      let count = 0;
      for (let n = from; n < to; n++) {

Bun's Worker API uses real OS threads, so CPU-bound work can be split across workers created from an in-memory blob: URL, with no separate worker file needed.

Node.jsadvanced

worker_threads for CPU-bound work off the main thread

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

const fibonacci = (n: number): number => (n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2));

if (isMainThread) {

Offload a genuinely CPU-heavy computation to a worker_threads Worker so it stops blocking the event loop, using one self-contained file.