Performance: Event-driven architecture
This affects how the server handles requests and internal events, impacting response time and resource usage.
app.get('/data', async (req, res) => { const result = await heavyAsyncOperation(); res.send(result); });
app.get('/data', (req, res) => {
const result = heavySyncOperation();
res.send(result);
});| Pattern | Event Loop Blocking | Async Handling | Response Time Impact | Verdict |
|---|---|---|---|---|
| Synchronous heavy tasks in event handlers | High (blocks event loop) | No | Increases response time | [X] Bad |
| Asynchronous heavy tasks in event handlers | Low (non-blocking) | Yes | Improves response time | [OK] Good |
EventEmitter in an Express app?EventEmitter is used to create and listen for events, enabling asynchronous reactions in the app.events module exports EventEmitter as a property, so you must access it before creating an instance.EventEmitter and creates a new instance. const EventEmitter = require('events'); const emitter = new EventEmitter(); misses the .EventEmitter property. Options A and C misuse the constructor call.const EventEmitter = require('events').EventEmitter;
const emitter = new EventEmitter();
emitter.on('greet', (name) => {
console.log(`Hello, ${name}!`);
});
emitter.emit('greet', 'Alice');emitter.emit('greet', 'Alice') runs, it triggers the listener, printing "Hello, Alice!".const EventEmitter = require('events').EventEmitter;
const emitter = new EventEmitter();
emitter.emit('start');
emitter.on('start', () => {
console.log('Started');
});emitter.emit('start') is called, so it misses the event.emit exists and callback syntax is valid.Pass the test and this pattern moves unknown