javascript / expert
Snippet
Zero-Copy Transfers via MessagePort
In Node.js Worker Threads, you can use a transfer list (the second argument to postMessage) to move memory instead of cloning it. This is highly efficient for large datasets as it eliminates the overhead of copying bytes between threads.
snippet.js
1
2
3
4
5
6
7
8
9
const { Worker, MessageChannel } = require('node:worker_threads');const { port1, port2 } = new MessageChannel();const buffer = new Uint8Array(1024 * 1024 * 100); // 100MB// Transfer the underlying ArrayBuffer without copying dataport1.postMessage({ data: buffer }, [buffer.buffer]);console.log(buffer.byteLength); // 0 (Memory was moved, not copied)
nodejs
Breakdown
1
postMessage({ data: buffer }, [buffer.buffer])
The second argument indicates which ArrayBuffers should be transferred.
2
buffer.byteLength
After transfer, the original buffer is detached and its length becomes zero.