0
0
Node.jsframework~20 mins

Passing data to workers in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Worker Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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);
  });
}
Aundefined
Bhello
CReferenceError
DNo output
Attempts:
2 left
💡 Hint
Think about how the main thread sends data to the worker using postMessage and how the worker listens for messages.
📝 Syntax
intermediate
2: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?
Anew Worker('worker.js', { data: { user: 'Alice' } })
Bnew Worker(new URL('worker.js', import.meta.url), { message: { user: 'Alice' } })
Cnew Worker('worker.js', { initialData: { user: 'Alice' } })
Dnew Worker(new URL('worker.js', import.meta.url), { workerData: { user: 'Alice' } })
Attempts:
2 left
💡 Hint
Check the official property name used to pass data at worker creation.
state_output
advanced
2: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);
}
A{}
Bundefined
C{ count: 5, active: true }
Dnull
Attempts:
2 left
💡 Hint
Remember that 'workerData' contains the data passed at worker creation.
🔧 Debug
advanced
2: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);
}
AworkerData is undefined because data was sent via postMessage, not workerData option
BpostMessage is not a function on worker
CparentPort is undefined in the worker
DThe worker script URL is incorrect
Attempts:
2 left
💡 Hint
Check how data is accessed inside the worker and how it was sent.
🧠 Conceptual
expert
3: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?
APass the ArrayBuffer via workerData with a TransferList containing the buffer
BSend the ArrayBuffer via postMessage without a TransferList
CSerialize the ArrayBuffer to JSON and send as a string
DWrite the buffer to a file and read it in the worker
Attempts:
2 left
💡 Hint
Think about zero-copy transfer of buffers in worker_threads.