✅ 1. Core Node.js Concepts - Event Loop Flashcards

Phases, call stack, and callback queue.

1
Q

What is the event loop in Node.js?

A

It’s a mechanism that handles asynchronous operations by offloading tasks from the call stack to the callback queue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the phases of the Node.js event loop?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the role of the call stack in Node.js?

A

It manages the execution of synchronous functions in a LIFO (Last In, First Out) manner.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What happens when the call stack is empty?

A

The event loop picks the next task from the callback queue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between setImmediate() and process.nextTick()?

A

setImmediate() runs in the check phase.
process.nextTick() runs before the next phase of the event loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which has higher priority: setTimeout() or setImmediate()?

A

If called from the main script, setImmediate() executes first.
If inside an I/O callback, setImmediate() executes after setTimeout().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the poll phase do in the event loop?

A

It waits for incoming I/O events and executes their callbacks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the output of this code?

setTimeout(() => console.log(‘Timeout’), 0);
setImmediate(() => console.log(‘Immediate’));

A

The output is non-deterministic.
Either Timeout or Immediate can log first, depending on execution time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does process.nextTick() affect the event loop?

A

It interrupts the event loop and executes the callback immediately after the current operation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What happens if you continuously call process.nextTick()?

A

It blocks the event loop, causing starvation of I/O operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the microtask queue in Node.js?

A

It stores callbacks for Promises and process.nextTick(), which have higher priority than the callback queue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the output of this code?

setTimeout(() => console.log(‘Timeout’), 0);
Promise.resolve().then(() => console.log(‘Promise’));

A

Promise
Timeout
Microtasks (Promises) execute before setTimeout().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happens when a function is added to the callback queue?

A

It waits until the call stack is empty before execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What phase handles setTimeout() callbacks?

A

The Timers phase.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does I/O handling work in the event loop?

A

I/O operations are handled in the poll phase, where their callbacks are executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the difference between the callback queue and microtask queue?

A

Microtask queue: Higher priority (Promises, process.nextTick()).
Callback queue: Lower priority (I/O callbacks, timers).

17
Q

What is the purpose of the check phase in the event loop?

A

It executes setImmediate() callbacks.

18
Q

What is the output of this code?

console.log(‘A’);
setImmediate(() => console.log(‘B’));
setTimeout(() => console.log(‘C’), 0);
console.log(‘D’);

19
Q

What happens if you execute a large synchronous loop in Node.js?

A

It blocks the event loop, preventing asynchronous tasks from executing.

20
Q

What is the purpose of the close phase?

A

It handles callbacks for closed resources like sockets or files.

21
Q

What does this code print?

setTimeout(() => console.log(‘Timeout’), 0);
process.nextTick(() => console.log(‘NextTick’));

A

NextTick
Timeout
process.nextTick() executes before the next event loop iteration.

22
Q

What is backpressure in the event loop?

A

It occurs when the producer writes data faster than the consumer can read.

23
Q

How does Node.js prevent blocking the event loop during I/O operations?

A

It uses asynchronous callbacks and offloads operations to worker threads.

24
Q

How do you measure the event loop delay?

A

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);

25
Q

What is the difference between setTimeout(fn, 0) and setImmediate(fn)?

A

setTimeout() runs in the timers phase.
setImmediate() runs in the check phase.

26
Q

What is the effect of running multiple setImmediate() calls?

A

They execute in FIFO order in the check phase.

27
Q

What happens if you block the event loop with a long-running synchronous function?

A

It delays the execution of asynchronous tasks.

28
Q

What is the difference between libuv and V8?

A

libuv: Handles I/O operations and the event loop.
V8: Executes JavaScript code.

29
Q

What happens if you use process.exit() inside an async function?

A

It terminates the process immediately, skipping pending callbacks.

30
Q

How can you block the event loop for testing purposes?

A

Using a while loop:

while (true) {} // Blocks the event loop