Challenge - 5 Problems
Worker Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What will this worker output?
Consider this Node.js worker code snippet. What will the worker print to the console?
Node.js
import { Worker, isMainThread, parentPort, workerData } from 'node:worker_threads'; if (isMainThread) { const worker = new Worker(new URL(import.meta.url)); worker.postMessage({ greeting: 'hello' }); } else { parentPort.on('message', (msg) => { console.log(msg.greeting); }); }
Attempts:
2 left
💡 Hint
Think about how the main thread sends data to the worker using postMessage and how the worker listens for messages.
✗ Incorrect
The main thread sends an object with a greeting property to the worker using postMessage. The worker listens for 'message' events and logs the greeting property, so it prints 'hello'.
📝 Syntax
intermediate2:00remaining
Which option correctly passes initial data to a worker?
You want to pass initial data to a worker thread at creation. Which code snippet correctly does this?
Attempts:
2 left
💡 Hint
Check the official property name used to pass data at worker creation.
✗ Incorrect
The correct option uses the 'workerData' property to pass initial data to the worker thread. Other options use invalid property names.
❓ state_output
advanced2:00remaining
What is the value of 'workerData' inside the worker?
Given this worker creation code, what will be the value of 'workerData' inside the worker thread?
Node.js
import { Worker, isMainThread, workerData } from 'node:worker_threads'; if (isMainThread) { new Worker(new URL(import.meta.url), { workerData: { count: 5, active: true } }); } else { console.log(workerData); }
Attempts:
2 left
💡 Hint
Remember that 'workerData' contains the data passed at worker creation.
✗ Incorrect
The workerData object inside the worker contains exactly what was passed in the main thread's Worker constructor.
🔧 Debug
advanced2:00remaining
Why does this worker not receive data?
This code intends to send data to the worker, but the worker never logs it. Why?
Node.js
import { Worker, isMainThread, parentPort, workerData } from 'node:worker_threads'; if (isMainThread) { const worker = new Worker(new URL(import.meta.url)); worker.postMessage('data'); } else { console.log(workerData); }
Attempts:
2 left
💡 Hint
Check how data is accessed inside the worker and how it was sent.
✗ Incorrect
workerData only contains data passed at creation. postMessage sends data asynchronously and must be received via parentPort.on('message').
🧠 Conceptual
expert3:00remaining
Which method correctly shares a large buffer efficiently between main and worker?
You want to share a large ArrayBuffer between the main thread and a worker without copying it. Which approach is correct?
Attempts:
2 left
💡 Hint
Think about zero-copy transfer of buffers in worker_threads.
✗ Incorrect
Passing the buffer with a TransferList transfers ownership without copying, making it efficient. Sending without TransferList copies the data. JSON serialization is slow and lossy. File I/O is inefficient.