Performance: Passing data to workers
MEDIUM IMPACT
This affects how quickly data is transferred between the main thread and worker threads, impacting responsiveness and CPU utilization.
const { Worker } = require('worker_threads');
const worker = new Worker('./worker.js');
const largeBuffer = new SharedArrayBuffer(4 * 1000000);
worker.postMessage({ buffer: largeBuffer });const { Worker } = require('worker_threads');
const worker = new Worker('./worker.js');
const largeData = { array: new Array(1000000).fill(0) };
worker.postMessage(largeData);| Pattern | Main Thread Block Time | Memory Copy | Serialization Cost | Verdict |
|---|---|---|---|---|
| Passing large objects by value | High (10-100ms) | High (~4MB) | High | [X] Bad |
| Passing SharedArrayBuffer | ~0ms | None | Low | [OK] Good |