Bird
0
0

You want to pass a large array to a worker thread without copying it to save memory. Which approach should you use?

hard📝 Application Q8 of 15
Node.js - Worker Threads
You want to pass a large array to a worker thread without copying it to save memory. Which approach should you use?
APass the array directly in workerData
BUse a SharedArrayBuffer and pass it via workerData
CSerialize the array to JSON and pass as string
DUse environment variables to share the array
Step-by-Step Solution
Solution:
  1. Step 1: Understand data copying in workers

    Passing normal arrays copies data, which uses more memory.
  2. Step 2: Use SharedArrayBuffer for shared memory

    SharedArrayBuffer allows sharing memory between threads without copying.
  3. Final Answer:

    Use a SharedArrayBuffer and pass it via workerData -> Option B
  4. Quick Check:

    Use SharedArrayBuffer to share large data efficiently [OK]
Quick Trick: Use SharedArrayBuffer to share memory without copying [OK]
Common Mistakes:
  • Passing large arrays directly causing copies
  • Using JSON serialization wasting memory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes