MEAN Node.js Async Patterns and Performance 1 — Questions and Answers
Question 1: What is the Node.js Event Loop and why is it important for MEAN stack applications?
- A single-threaded mechanism that processes async callbacks without blocking, enabling high concurrency (Correct answer)
- A loop that processes DOM events in the browser
- A queue that manages database transactions sequentially
- A multi-threaded scheduler for parallel task execution
Correct answer: A single-threaded mechanism that processes async callbacks without blocking, enabling high concurrency
Node.js's event loop processes I/O callbacks, timers, and promises on a single thread without blocking, making it efficient for high-concurrency API servers.
Question 2: What is the difference between `process.nextTick()` and `setImmediate()` in Node.js?
- nextTick() fires before I/O callbacks in the current iteration; setImmediate() fires after I/O callbacks in the next iteration (Correct answer)
- nextTick() fires after I/O callbacks; setImmediate() fires before I/O callbacks
- Both fire at the same time but nextTick() has higher priority for CPU tasks
- nextTick() is synchronous; setImmediate() is asynchronous
Correct answer: nextTick() fires before I/O callbacks in the current iteration; setImmediate() fires after I/O callbacks in the next iteration
process.nextTick() callbacks run before any I/O events in the current event loop iteration, while setImmediate() runs in the check phase of the next iteration.
Question 3: What are Node.js Worker Threads used for?
- Running CPU-intensive JavaScript in parallel on separate threads without blocking the event loop (Correct answer)
- Managing worker processes in a PM2 cluster
- Handling database connection pooling
- Processing web socket connections concurrently
Correct answer: Running CPU-intensive JavaScript in parallel on separate threads without blocking the event loop
Worker Threads (worker_threads module) allow Node.js to run JavaScript in parallel threads, ideal for CPU-bound tasks that would otherwise block the event loop.
Question 4: What does `Promise.all()` do in Node.js async programming?
- Runs multiple Promises in parallel and resolves when all complete or rejects when any fails (Correct answer)
- Runs Promises sequentially and collects all results
- Retries failed Promises automatically until all succeed
- Cancels all Promises if one fails
Correct answer: Runs multiple Promises in parallel and resolves when all complete or rejects when any fails
Promise.all() takes an array of Promises, starts them all simultaneously, and returns a single Promise that resolves with an array of all results when every Promise resolves.
Question 5: What is Node.js clustering and how does it relate to a MEAN stack API server?
- Creates multiple worker processes that share the same server port to utilize all CPU cores (Correct answer)
- Groups Node.js instances behind a load balancer on different ports
- Clusters database connections for better performance
- Creates redundant server instances for failover
Correct answer: Creates multiple worker processes that share the same server port to utilize all CPU cores
The cluster module forks multiple worker processes (one per CPU core) that all listen on the same port, allowing Node.js to handle more concurrent requests.
Question 6: What is a memory leak in Node.js and what is a common cause in Express applications?
- When objects are kept in memory indefinitely, commonly caused by event listeners that are never removed (Correct answer)
- When the heap exceeds 1.5GB and crashes the process
- When garbage collection pauses the event loop for too long
- When stream buffers aren't flushed after responding
Correct answer: When objects are kept in memory indefinitely, commonly caused by event listeners that are never removed
Memory leaks occur when objects remain referenced and can't be garbage collected; in Express apps, accumulating event listeners or growing arrays/maps are common culprits.
What is the Node.js Event Loop and why is it important for MEAN stack applications?