Offload functions to worker threads with shared memory primitives for Node.js.
8
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: add transfer example showing zero-copy buffer passing

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

+41
+31
examples/transfer/main.ts
··· 1 + // Zero-copy transfer of large buffers to and from workers. 2 + // The input is transferred (moved) to the worker — no copy on send. 3 + // The result is auto-transferred back — no copy on return. 4 + // Requires Node v24+. 5 + // 6 + // Run: node examples/transfer/main.ts 7 + 8 + import { workers, transfer } from '../../src/index.ts'; 9 + import { processImage } from './process-image.ts'; 10 + 11 + // Simulate a 1MB image 12 + const size = 1_000_000; 13 + const pixels = new Uint8Array(size); 14 + for (let i = 0; i < size; i++) { 15 + pixels[i] = i % 256; 16 + } 17 + 18 + console.log(`Image size: ${size} bytes`); 19 + console.log(`First 5 pixels: [${[...pixels.slice(0, 5)]}]`); 20 + 21 + { 22 + using run = workers(1); 23 + 24 + const start = performance.now(); 25 + const result = await run(processImage(transfer(pixels))); 26 + const elapsed = (performance.now() - start).toFixed(1); 27 + 28 + console.log(`\nProcessed in ${elapsed}ms (zero-copy transfer)`); 29 + console.log(`First 5 result: [${[...result.slice(0, 5)]}]`); 30 + console.log(`\nOriginal buffer detached: ${pixels.buffer.byteLength === 0}`); 31 + }
+10
examples/transfer/process-image.ts
··· 1 + import { mo } from '../../src/index.ts'; 2 + 3 + export const processImage = mo(import.meta, (pixels: Uint8Array): Uint8Array => { 4 + // Invert each pixel (simulate image processing) 5 + const result = new Uint8Array(pixels.length); 6 + for (let i = 0; i < pixels.length; i++) { 7 + result[i] = 255 - pixels[i]; 8 + } 9 + return result; 10 + });