Performance: Why the event system matters
This affects how efficiently the application handles user interactions and asynchronous tasks, impacting responsiveness and CPU usage.
Jump into concepts and practice - no test required
const http = require('http'); const server = http.createServer(async (req, res) => { // Use asynchronous non-blocking operations await new Promise(resolve => setImmediate(resolve)); res.end('Done'); }); server.listen(3000);
const http = require('http'); const server = http.createServer((req, res) => { // Heavy synchronous processing on each request for (let i = 0; i < 1e9; i++) {} res.end('Done'); }); server.listen(3000);
| Pattern | Event Loop Blocking | CPU Usage | Responsiveness | Verdict |
|---|---|---|---|---|
| Heavy synchronous processing | Blocks event loop | High CPU spike | Poor, delayed responses | [X] Bad |
| Asynchronous non-blocking code | Event loop free | Balanced CPU usage | Fast, responsive | [OK] Good |
data on an EventEmitter instance emitter?on, not listen, addEvent, or catch.emitter.on('data', callback) is standard and valid in Node.js.on to listen for events [OK]const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.on('greet', () => {
console.log('Hello!');
});
emitter.emit('greet');
emitter.emit('greet');const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.on('update', function() {
console.log('Update received');
});
emitter.emit('updates');message event and counts how many times it happens. Which approach best uses the event system to keep the count updated and print it after 3 messages?message fires, then check the count inside the listener to print after 3. -> Option D