✅ 1. Core Node.js Concepts - Event Loop Flashcards
Phases, call stack, and callback queue.
What is the event loop in Node.js?
It’s a mechanism that handles asynchronous operations by offloading tasks from the call stack to the callback queue.
What are the phases of the Node.js event loop?
Timers: Executes setTimeout() and setInterval() callbacks.
Pending callbacks: Executes I/O callbacks.
Idle, prepare: Used internally.
Poll: Retrieves new I/O events.
Check: Executes setImmediate() callbacks.
Close callbacks: Executes close event callbacks.
What is the role of the call stack in Node.js?
It manages the execution of synchronous functions in a LIFO (Last In, First Out) manner.
What happens when the call stack is empty?
The event loop picks the next task from the callback queue.
What is the difference between setImmediate() and process.nextTick()?
setImmediate() runs in the check phase.
process.nextTick() runs before the next phase of the event loop.
Which has higher priority: setTimeout() or setImmediate()?
If called from the main script, setImmediate() executes first.
If inside an I/O callback, setImmediate() executes after setTimeout().
What does the poll phase do in the event loop?
It waits for incoming I/O events and executes their callbacks.
What is the output of this code?
setTimeout(() => console.log(‘Timeout’), 0);
setImmediate(() => console.log(‘Immediate’));
The output is non-deterministic.
Either Timeout or Immediate can log first, depending on execution time.
How does process.nextTick() affect the event loop?
It interrupts the event loop and executes the callback immediately after the current operation.
What happens if you continuously call process.nextTick()?
It blocks the event loop, causing starvation of I/O operations.
What is the microtask queue in Node.js?
It stores callbacks for Promises and process.nextTick(), which have higher priority than the callback queue.
What is the output of this code?
setTimeout(() => console.log(‘Timeout’), 0);
Promise.resolve().then(() => console.log(‘Promise’));
Promise
Timeout
Microtasks (Promises) execute before setTimeout().
What happens when a function is added to the callback queue?
It waits until the call stack is empty before execution.
What phase handles setTimeout() callbacks?
The Timers phase.
How does I/O handling work in the event loop?
I/O operations are handled in the poll phase, where their callbacks are executed.
What is the difference between the callback queue and microtask queue?
Microtask queue: Higher priority (Promises, process.nextTick()).
Callback queue: Lower priority (I/O callbacks, timers).
What is the purpose of the check phase in the event loop?
It executes setImmediate() callbacks.
What is the output of this code?
console.log(‘A’);
setImmediate(() => console.log(‘B’));
setTimeout(() => console.log(‘C’), 0);
console.log(‘D’);
A
D
C
B
What happens if you execute a large synchronous loop in Node.js?
It blocks the event loop, preventing asynchronous tasks from executing.
What is the purpose of the close phase?
It handles callbacks for closed resources like sockets or files.
What does this code print?
setTimeout(() => console.log(‘Timeout’), 0);
process.nextTick(() => console.log(‘NextTick’));
NextTick
Timeout
process.nextTick() executes before the next event loop iteration.
What is backpressure in the event loop?
It occurs when the producer writes data faster than the consumer can read.
How does Node.js prevent blocking the event loop during I/O operations?
It uses asynchronous callbacks and offloads operations to worker threads.
How do you measure the event loop delay?
Using perf_hooks:
const { performance, PerformanceObserver } = require(‘perf_hooks’);
const obs = new PerformanceObserver((items) => {
console.log(items.getEntries()[0].duration);
});
obs.observe({ entryTypes: [‘measure’] });
performance.mark(‘start’);
setTimeout(() => performance.mark(‘end’), 100);
What is the difference between setTimeout(fn, 0) and setImmediate(fn)?
setTimeout() runs in the timers phase.
setImmediate() runs in the check phase.
What is the effect of running multiple setImmediate() calls?
They execute in FIFO order in the check phase.
What happens if you block the event loop with a long-running synchronous function?
It delays the execution of asynchronous tasks.
What is the difference between libuv and V8?
libuv: Handles I/O operations and the event loop.
V8: Executes JavaScript code.
What happens if you use process.exit() inside an async function?
It terminates the process immediately, skipping pending callbacks.
How can you block the event loop for testing purposes?
Using a while loop:
while (true) {} // Blocks the event loop