0
0
Node.jsframework~20 mins

Why worker threads matter in Node.js - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Worker Threads Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use worker threads in Node.js?
Which of the following best explains why worker threads are important in Node.js?
AThey automatically optimize memory usage for all Node.js applications.
BThey replace the need for asynchronous callbacks in Node.js.
CThey allow Node.js to run multiple JavaScript operations in parallel without blocking the main thread.
DThey enable Node.js to run on multiple machines at the same time.
Attempts:
2 left
💡 Hint
Think about how Node.js handles tasks and what happens when a task takes a long time.
component_behavior
intermediate
2:00remaining
Output of worker thread example
What will be the output of this Node.js code using worker threads?
Node.js
const { Worker, isMainThread, parentPort } = require('worker_threads');

if (isMainThread) {
  const worker = new Worker(__filename);
  worker.on('message', message => console.log('From worker:', message));
  console.log('Main thread running');
} else {
  parentPort.postMessage('Hello from worker');
}
AMain thread running
BMain thread running\nFrom worker: Hello from worker
CFrom worker: Hello from worker\nMain thread running
DFrom worker: Hello from worker
Attempts:
2 left
💡 Hint
Consider the order of console.log calls in main and worker threads.
🔧 Debug
advanced
2:00remaining
Identify the error in worker thread usage
What error will this Node.js code produce?
Node.js
const { Worker } = require('worker_threads');

const worker = new Worker('console.log("Hello")');
ASyntaxError: Unexpected token 'console.log("Hello")'
BReferenceError: Worker is not defined
CNo error, prints 'Hello'
DTypeError: Worker constructor expects a file path or URL
Attempts:
2 left
💡 Hint
Check what the Worker constructor expects as input.
state_output
advanced
2:00remaining
State sharing between main and worker threads
Given this code, what will be the final value of 'counter' in the main thread?
Node.js
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');

let counter = 0;

if (isMainThread) {
  const worker = new Worker(__filename, { workerData: counter });
  worker.on('message', value => {
    counter = value;
    console.log('Counter in main:', counter);
  });
} else {
  let localCounter = workerData;
  localCounter += 5;
  parentPort.postMessage(localCounter);
}
ACounter in main: 5
BCounter in main: 1
CCounter in main: undefined
DCounter in main: 0
Attempts:
2 left
💡 Hint
Remember that workerData is a copy, not shared memory.
📝 Syntax
expert
2:00remaining
Correct syntax to create a worker thread with inline code
Which option correctly creates a worker thread that runs inline JavaScript code in Node.js?
Anew Worker(new URL('data:text/javascript,console.log("Hello")'), { type: 'module' });
Bnew Worker('console.log("Hello")');
Cnew Worker(() => console.log('Hello'));
Dnew Worker({ code: 'console.log("Hello")' });
Attempts:
2 left
💡 Hint
Worker constructor accepts a file path, URL, or data URL with type module.