Offload functions to worker threads with shared memory primitives for Node.js.
1// CPU-bound prime checking offloaded to a dedicated worker thread.
2// Requires Node v24+.
3//
4// Run: node examples/primes/main.ts
5
6import { isPrime } from './is-prime.ts';
7
8const candidates = [999_999_937, 1_000_000_007, 1_000_000_009, 1_000_000_021, 1_000_000_033, 999_999_938];
9
10for (const n of candidates) {
11 const prime = await isPrime(n);
12 console.log(`${n} is ${prime ? 'prime' : 'not prime'}`);
13}