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 a Worker Thread in Node.js?
A Worker Thread is a way to run JavaScript code in parallel within the same Node.js process. It shares memory with the main thread but runs independently to perform CPU-intensive tasks without blocking the main event loop.
Click to reveal answer
beginner
What is a Child Process in Node.js?
A Child Process is a separate process created by the main Node.js process. It runs independently with its own memory and event loop, allowing you to run external programs or scripts without blocking the main process.
Click to reveal answer
intermediate
How do Worker Threads and Child Processes differ in memory usage?
Worker Threads share memory with the main thread using SharedArrayBuffer, which allows faster communication. Child Processes have separate memory spaces, so communication happens via inter-process messaging, which is slower.
Click to reveal answer
intermediate
When should you use a Worker Thread instead of a Child Process?
Use Worker Threads for CPU-heavy JavaScript tasks that need to run in parallel but share data efficiently. They are lighter and faster for tasks within Node.js. Use Child Processes when you need to run external programs or isolate tasks completely.
Click to reveal answer
advanced
What is a key limitation of Worker Threads compared to Child Processes?
Worker Threads run in the same process, so if a Worker Thread crashes, it can affect the main process. Child Processes are isolated, so a crash in a child does not crash the main process.
Click to reveal answer
Which Node.js feature runs JavaScript code in parallel within the same process?
AEvent Loop
BChild Process
CWorker Thread
DCallback Function
✗ Incorrect
Worker Threads run JavaScript code in parallel inside the same Node.js process.
Which runs in a separate memory space and process?
AWorker Thread
BChild Process
CPromise
DAsync Function
✗ Incorrect
Child Processes run in separate processes with their own memory.
Which is better for running external programs from Node.js?
AChild Process
BsetTimeout
CWorker Thread
DEvent Loop
✗ Incorrect
Child Processes are designed to run external programs or scripts.
B. Wrong module imported, should be 'child_process'
C. Missing new keyword before Worker constructor
D. Worker threads cannot be created with a filename
Solution
Step 1: Check Worker thread creation syntax
The Worker class must be instantiated with the new keyword.
Step 2: Identify error in code
The code calls Worker('worker.js') without new, causing a TypeError.
Final Answer:
Missing new keyword before Worker constructor -> Option C
Quick Check:
Use new Worker() to create threads [OK]
Hint: Always use new with Worker() constructor [OK]
Common Mistakes:
Forgetting new keyword
Importing wrong module for threads
Thinking worker.js must be JSON
5. You want to perform a CPU-heavy task in Node.js without blocking the main event loop. Which approach is best and why?
hard
A. Use a child process to run the task in a separate process communicating via messages.
B. Use a worker thread to run the task sharing memory with the main thread.
C. Run the task directly in the main thread asynchronously.
D. Use setTimeout to delay the task execution in the main thread.
Solution
Step 1: Understand CPU-heavy task impact
CPU-heavy tasks block the main event loop if run directly, causing app unresponsiveness.
Step 2: Compare worker threads and child processes for heavy tasks
Worker threads share memory but still run in the same process, which can cause contention. Child processes run in separate processes, isolating CPU load and preventing blocking.
Step 3: Evaluate other options
Running asynchronously in main thread or delaying with setTimeout does not prevent blocking for CPU-heavy tasks.
Final Answer:
Use a child process to run the task in a separate process communicating via messages. -> Option A
Quick Check:
Heavy CPU tasks best isolated in child processes [OK]
Hint: Heavy CPU tasks? Use child processes to avoid blocking [OK]