Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of worker threads in Node.js?
Worker threads allow Node.js to run JavaScript code in parallel on multiple threads, helping to perform CPU-intensive tasks without blocking the main event loop.
Click to reveal answer
beginner
How do you create a new worker thread in Node.js?
You create a new worker thread by importing the Worker class from the 'worker_threads' module and then instantiating it with a path to a JavaScript file to run in the worker.
Click to reveal answer
intermediate
What method is used to send messages between the main thread and a worker thread?
The 'postMessage' method is used to send messages, and the 'message' event is listened to receive messages between the main thread and worker threads.
Click to reveal answer
beginner
Why should you avoid blocking the main thread in Node.js?
Blocking the main thread stops Node.js from handling other tasks like I/O or user requests, making the app unresponsive. Worker threads help by running heavy tasks separately.
Click to reveal answer
intermediate
What is the difference between worker threads and child processes in Node.js?
Worker threads share memory and are lighter weight, making communication faster. Child processes run in separate memory spaces and are heavier but can run different programs.
Click to reveal answer
Which module do you import to use worker threads in Node.js?
A'worker_threads'
B'cluster'
C'child_process'
D'events'
✗ Incorrect
The 'worker_threads' module provides the Worker class to create worker threads.
How do you send data from the main thread to a worker thread?
AUsing the 'postMessage' method
BUsing the 'send' method
CUsing the 'emit' method
DUsing the 'write' method
✗ Incorrect
The 'postMessage' method sends messages between the main thread and worker threads.
What happens if you run a CPU-heavy task on the main thread in Node.js?
AThe app becomes faster
BThe app blocks and becomes unresponsive
CNothing changes
DThe task runs in parallel automatically
✗ Incorrect
CPU-heavy tasks block the main thread, making the app unresponsive.
Which event do you listen to in a worker thread to receive messages?
A'data'
B'ready'
C'connect'
D'message'
✗ Incorrect
The 'message' event is used to receive messages in worker threads.
What is a key advantage of worker threads over child processes?
AThey run in separate memory spaces
BThey can run different programs
CThey share memory and have faster communication
DThey are slower but more secure
✗ Incorrect
Worker threads share memory, making communication faster and lighter weight than child processes.
Explain how to create and communicate with a worker thread in Node.js.
Think about how the main thread and worker thread talk to each other.
You got /4 concepts.
Describe why worker threads are useful in Node.js applications.
Consider what happens when heavy tasks run on the main thread.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using worker_threads in Node.js?
easy
A. To handle HTTP requests faster
B. To simplify asynchronous callbacks
C. To manage database connections
D. To run code in parallel without blocking the main thread
Solution
Step 1: Understand worker threads role
Worker threads allow running JavaScript code in parallel threads separate from the main thread.
Step 2: Compare with other options
Options B, C, and D describe unrelated tasks; worker threads specifically help avoid blocking the main thread.
Final Answer:
To run code in parallel without blocking the main thread -> Option D
Quick Check:
Parallel code execution = A [OK]
Hint: Worker threads run code parallel to main thread [OK]
Common Mistakes:
Confusing worker threads with async callbacks
Thinking worker threads manage HTTP or DB tasks directly
2. Which of the following is the correct way to import the Worker class from the worker_threads module?
easy
A. const Worker = require('worker_threads');
B. import Worker from 'worker_threads';
C. const { Worker } = require('worker_threads');
D. const Worker = require('worker_threads').worker;
Solution
Step 1: Recall correct import syntax
The Worker class is a named export, so we use destructuring: const { Worker } = require('worker_threads');
Step 2: Check other options
const Worker = require('worker_threads'); assigns the entire module object, not the Worker class. import Worker from 'worker_threads'; uses ES module syntax which requires extra config. const Worker = require('worker_threads').worker; uses wrong property name.
Final Answer:
const { Worker } = require('worker_threads'); -> Option C
Quick Check:
Destructure Worker from module = D [OK]
Hint: Use destructuring to import Worker from worker_threads [OK]
Common Mistakes:
Using wrong property name like 'worker' instead of 'Worker'
Mixing CommonJS and ES module syntax incorrectly
3. What will be the output of the following code snippet?
The Worker constructor expects an absolute path or a URL, not just a relative string like 'worker.js'.
Step 2: Validate other options
Cannot create Worker with a string filename is incorrect because Worker can be created with a filename if path is correct. Missing event listener for 'error' event is good practice but not an error. Cannot call postMessage on Worker instance is wrong; postMessage is valid on Worker.
Final Answer:
Worker file path should be absolute or URL -> Option A
Quick Check:
Worker needs absolute path or URL = C [OK]
Hint: Use absolute path or URL for Worker file [OK]
Common Mistakes:
Using relative paths without resolving them
Ignoring error event listeners
Thinking postMessage is invalid on Worker
5. You want to create a worker thread that performs a CPU-heavy calculation and sends the result back. Which approach correctly creates the worker and handles the result asynchronously?
hard
A. Use new Worker() without arguments and call worker.send() to communicate
B. Use new Worker('./calc.js') with worker.on('message', callback) and send data via worker.postMessage()
C. Use require('worker_threads').run() to start the worker and get a promise
D. Create a child process with child_process.fork() and communicate with worker.postMessage()
Solution
Step 1: Identify correct Worker creation and communication
Creating a worker with new Worker('./calc.js') and using worker.on('message') plus worker.postMessage() is the standard pattern.
Step 2: Eliminate incorrect options
Use new Worker() without arguments and call worker.send() to communicate is invalid because Worker requires a filename or code. Use require('worker_threads').run() to start the worker and get a promise is incorrect; no run() method exists. Create a child process with child_process.fork() and communicate with worker.postMessage() uses child_process, not worker_threads, and postMessage is not valid on child processes.
Final Answer:
Use new Worker('./calc.js') with worker.on('message', callback) and send data via worker.postMessage() -> Option B
Quick Check:
Worker with filename + message events = A [OK]
Hint: Use new Worker(filename) and message events for communication [OK]