Performance: Event loop mental model
This concept affects how quickly Node.js can respond to events and handle asynchronous tasks without blocking the main thread.
Jump into concepts and practice - no test required
const fs = require('fs'); fs.readFile('largefile.txt', (err, data) => { if (err) throw err; console.log('File read complete'); });
const fs = require('fs'); const data = fs.readFileSync('largefile.txt'); console.log('File read complete');
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous blocking code | N/A | N/A | N/A | [X] Bad |
| Asynchronous non-blocking code | N/A | N/A | N/A | [OK] Good |
Promise callbacks before timers?setTimeout schedules a function after a specified delay in milliseconds.setTimeout(myFunc, 0) runs myFunc after the current call stack is empty, effectively scheduling it soon.console.log('start');
setTimeout(() => console.log('timeout'), 0);
Promise.resolve().then(() => console.log('promise'));
console.log('end');console.log('start') and console.log('end') run immediately (synchronously). setTimeout callback runs later. Promise callback runs as microtask after current stack.setTimeout(() => console.log('timeout'));
process.nextTick(() => console.log('nextTick'));
Promise.resolve().then(() => console.log('promise'));process.nextTick callbacks run immediately after the current operation, before promise microtasks and timers.process.nextTick callbacks have higher priority and run first, which can surprise learners expecting promises first.setTimeout allows the event loop to process other events between chunks, keeping responsiveness.