Bird
0
0

You need to run a CPU-intensive task in Node.js and efficiently share a large binary buffer between threads. Which approach is most suitable?

hard📝 Conceptual Q8 of 15
Node.js - Worker Threads
You need to run a CPU-intensive task in Node.js and efficiently share a large binary buffer between threads. Which approach is most suitable?
ARun the task in the main thread to avoid overhead.
BUse child processes and serialize the buffer via JSON for communication.
CUse worker threads with SharedArrayBuffer to share memory efficiently.
DUse child processes and share memory via global variables.
Step-by-Step Solution
Solution:
  1. Step 1: Consider CPU-intensive tasks

    Main thread should not be blocked; offload to worker threads or child processes.
  2. Step 2: Efficient data sharing

    Worker threads can share memory using SharedArrayBuffer without copying data.
  3. Step 3: Child processes require serialization

    Child processes communicate via IPC, which involves copying data and overhead.
  4. Final Answer:

    Worker threads with SharedArrayBuffer are best. -> Option C
  5. Quick Check:

    SharedArrayBuffer enables efficient memory sharing [OK]
Quick Trick: Use SharedArrayBuffer with worker threads for large data [OK]
Common Mistakes:
  • Using JSON serialization for large buffers
  • Running CPU-heavy tasks on main thread
  • Assuming global variables share memory across processes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes