0
0
Expressframework~8 mins

Response time tracking middleware in Express - Performance & Optimization

Choose your learning style9 modes available
Performance: Response time tracking middleware
MEDIUM IMPACT
This affects the server response time measurement, which indirectly impacts perceived page speed and user experience.
Measuring server response time for each HTTP request
Express
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();
});
Uses high-resolution timer for accuracy and defers logging asynchronously to avoid blocking the event loop.
📈 Performance GainReduces blocking time on event loop; more accurate timing; minimal CPU overhead
Measuring server response time for each HTTP request
Express
app.use((req, res, next) => {
  const start = Date.now();
  res.on('finish', () => {
    const duration = Date.now() - start;
    console.log(`Response time: ${duration}ms`);
  });
  next();
});
Using Date.now() can be less precise and may add slight overhead; logging on every request can slow down response handling under high load.
📉 Performance CostAdds small CPU overhead per request; logging can block event loop if synchronous
Performance Comparison
PatternCPU OverheadEvent Loop BlockingLogging ImpactVerdict
Date.now() with synchronous loggingLowYes, blocks event loopHigh under load[X] Bad
perf_hooks.performance.now() with async loggingVery lowNo, non-blockingMinimal[OK] Good
Rendering Pipeline
Response time tracking middleware runs during the server request lifecycle before sending the response. It measures time between request start and response finish, then logs or records it without blocking the event loop.
Request Handling
Event Loop
Logging
⚠️ BottleneckSynchronous logging or heavy computation in middleware can block the event loop, delaying response sending.
Core Web Vital Affected
INP
This affects the server response time measurement, which indirectly impacts perceived page speed and user experience.
Optimization Tips
1Use high-resolution timers like performance.now() for accurate timing.
2Avoid synchronous logging inside middleware to prevent event loop blocking.
3Defer logging asynchronously to keep server response fast and responsive.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of synchronous logging in response time middleware?
AIt blocks the event loop, delaying responses
BIt increases memory usage significantly
CIt causes layout shifts in the browser
DIt reduces network bandwidth
DevTools: Network and Performance panels
How to check: Use the Network panel to measure server response times and the Performance panel to check if server-side logging blocks event loop by analyzing flame charts.
What to look for: Look for long server response times and blocking tasks in the flame chart that correspond to middleware execution.