Performance: Response time tracking middleware
MEDIUM IMPACT
This affects the server response time measurement, which indirectly impacts perceived page speed and user experience.
import { performance } from 'perf_hooks'; app.use((req, res, next) => { const start = performance.now(); res.once('finish', () => { const duration = performance.now() - start; process.nextTick(() => console.log(`Response time: ${duration.toFixed(2)}ms`)); }); next(); });
app.use((req, res, next) => {
const start = Date.now();
res.on('finish', () => {
const duration = Date.now() - start;
console.log(`Response time: ${duration}ms`);
});
next();
});| Pattern | CPU Overhead | Event Loop Blocking | Logging Impact | Verdict |
|---|---|---|---|---|
| Date.now() with synchronous logging | Low | Yes, blocks event loop | High under load | [X] Bad |
| perf_hooks.performance.now() with async logging | Very low | No, non-blocking | Minimal | [OK] Good |