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
Worker Thread vs Child Process in Node.js
📖 Scenario: You are building a Node.js application that needs to perform heavy calculations without blocking the main program. You want to learn how to use worker_threads and child_process modules to run tasks in parallel.
🎯 Goal: Create a simple Node.js app that uses a worker thread and a child process to run the same calculation separately. You will see how to set up both and send messages between the main program and these parallel workers.
📋 What You'll Learn
Create a worker thread using the worker_threads module
Create a child process using the child_process module
Send a number from the main thread to both the worker thread and child process
Receive the squared result back from both and log it
Use exact variable and function names as instructed
💡 Why This Matters
🌍 Real World
Node.js apps often need to run heavy tasks without freezing the main program. Worker threads and child processes help by running code in parallel.
💼 Career
Understanding these parallel processing methods is important for backend developers to build efficient, responsive Node.js applications.
Progress0 / 4 steps
1
Set up the main data and imports
Create a variable called number and set it to 7. Import Worker from worker_threads and fork from child_process.
Node.js
Hint
Use const number = 7; and import Worker and fork exactly as shown.
2
Create a worker thread to calculate square
Create a new Worker instance called worker that runs a file named 'worker.js'. This worker will receive the number and calculate its square.
Node.js
Hint
Use new Worker('./worker.js') to create the worker thread.
3
Create a child process to calculate square
Create a child process called child using fork to run a file named 'child.js'. This child process will also receive the number and calculate its square.
Node.js
Hint
Use fork('./child.js') to create the child process.
4
Send number and receive results from worker and child
Send the number to both worker and child. Listen for messages from both using worker.on('message', ...) and child.on('message', ...). Log the results with console.log using the exact text: "Worker result: " and "Child result: " followed by the squared number.
Node.js
Hint
Use postMessage and send to send data. Use on('message') to receive and console.log to print results.
Practice
(1/5)
1. Which statement best describes the difference between worker threads and child processes in Node.js?
easy
A. Worker threads run in the same process sharing memory, while child processes run in separate processes with separate memory.
B. Worker threads run separate programs, child processes share the same memory space.
C. Both worker threads and child processes run in the same process and share memory.
D. Child processes run inside worker threads to improve performance.
Solution
Step 1: Understand worker threads behavior
Worker threads run JavaScript code in parallel but inside the same Node.js process and share memory.
Step 2: Understand child processes behavior
Child processes run completely separate programs with their own memory space and communicate via messages.
Final Answer:
Worker threads run in the same process sharing memory, while child processes run in separate processes with separate memory. -> Option A
Quick Check:
Worker threads share memory, child processes do not [OK]
Hint: Remember: threads share memory, processes do not [OK]
Common Mistakes:
Confusing memory sharing between threads and processes
Thinking child processes share memory
Assuming worker threads run separate programs
2. Which of the following is the correct way to create a worker thread in Node.js?
easy
A. const worker = spawn('worker.js');
B. const worker = fork('worker.js');
C. const worker = createThread('worker.js');
D. const worker = new Worker('worker.js');
Solution
Step 1: Recall worker thread creation syntax
Worker threads are created using the Worker class from the worker_threads module.
Step 2: Identify correct constructor usage
The correct syntax is new Worker('filename'). The fork and spawn methods are for child processes.
Final Answer:
const worker = new Worker('worker.js'); -> Option D
Quick Check:
Worker threads use new Worker() [OK]
Hint: Use new Worker() for threads, fork/spawn for processes [OK]
Common Mistakes:
Using fork() to create worker threads
Using spawn() for worker threads
Using non-existent createThread() function
3. Consider this Node.js code snippet using a child process:
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]