0
0
Expressframework~8 mins

Error-handling middleware in Express - Performance & Optimization

Choose your learning style9 modes available
Performance: Error-handling middleware
MEDIUM IMPACT
This affects server response time and user experience by managing errors efficiently without blocking the event loop.
Handling errors in Express routes
Express
app.get('/data', (req, res, next) => {
  try {
    const data = getDataSync();
    if (!data) {
      throw new Error('Data not found');
    }
    res.send(data);
  } catch (err) {
    next(err);
  }
});

app.use((err, req, res, next) => {
  res.status(500).send({ error: err.message });
});
Using error-handling middleware catches errors synchronously and sends proper responses without crashing the server.
📈 Performance GainPrevents event loop blocking and improves server uptime and user experience.
Handling errors in Express routes
Express
app.get('/data', (req, res) => {
  const data = getDataSync();
  if (!data) {
    throw new Error('Data not found');
  }
  res.send(data);
});
Throwing errors directly inside route handlers without middleware causes unhandled exceptions and crashes the server.
📉 Performance CostBlocks event loop on error, causing server downtime and poor responsiveness.
Performance Comparison
PatternEvent Loop BlockingError HandlingServer StabilityVerdict
Throw error directly in routeBlocks event loop on errorNo centralized handlingCrashes server[X] Bad
Use try-catch and next(err) in sync routesNon-blockingCentralized handlingStable server[OK] Good
Throw error in async route without nextUncaught promise rejectionNo handlingPotential memory leaks[X] Bad
Use async try-catch with next(err)Non-blockingCentralized handlingStable server[OK] Good
Rendering Pipeline
Error-handling middleware intercepts errors during request processing and sends responses without blocking the event loop or crashing the server.
Request Handling
Response Sending
⚠️ BottleneckUnhandled errors causing event loop blocking or server crashes
Core Web Vital Affected
INP
This affects server response time and user experience by managing errors efficiently without blocking the event loop.
Optimization Tips
1Always use error-handling middleware with next(err) to avoid blocking the event loop.
2Wrap async route handlers in try-catch and call next(err) to catch promise rejections.
3Avoid throwing errors directly in routes without middleware to maintain server stability.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of throwing errors directly inside Express route handlers without error middleware?
AIt causes layout shifts in the browser
BIt blocks the event loop and crashes the server
CIt increases bundle size
DIt slows down CSS rendering
DevTools: Network and Console panels
How to check: Open DevTools, go to Network to check server responses for error status codes; check Console for unhandled promise rejections or error logs.
What to look for: Look for 500 status responses handled gracefully and absence of unhandled promise rejection warnings.