0
0
Expressframework~8 mins

Error response formatting in Express - Performance & Optimization

Choose your learning style9 modes available
Performance: Error response formatting
MEDIUM IMPACT
This affects the speed of sending error responses and the user's perception of app responsiveness during failures.
Sending error responses in an Express app
Express
app.use((err, req, res, next) => {
  res.status(500).json({ error: 'Internal Server Error' });
});
Sends minimal JSON error message, reducing response size and parsing time on client.
📈 Performance GainSaves 5-10kb response size, reduces blocking time by 50-100ms
Sending error responses in an Express app
Express
app.use((err, req, res, next) => {
  res.status(500).send(`<html><body><h1>Error</h1><pre>${err.stack}</pre></body></html>`);
});
Sending full HTML with stack trace increases response size and blocks rendering longer on client.
📉 Performance CostAdds 5-10kb to response size, blocking rendering for extra 50-100ms on slow networks
Performance Comparison
PatternResponse SizeParsing TimeRendering DelayVerdict
Full HTML error with stack traceLarge (5-10kb+)HighHigh (blocks rendering)[X] Bad
Minimal JSON error messageSmall (<1kb)LowLow (fast render)[OK] Good
Rendering Pipeline
Error response formatting affects the server-to-client data transfer and client rendering stages. Large or complex error responses increase network transfer time and client parsing, delaying the next paint.
Network Transfer
Client Parsing
Paint
⚠️ BottleneckNetwork Transfer and Client Parsing
Core Web Vital Affected
INP
This affects the speed of sending error responses and the user's perception of app responsiveness during failures.
Optimization Tips
1Send minimal error data to reduce response size.
2Avoid sending full HTML with stack traces in production.
3Use JSON error responses for faster client parsing and rendering.
Performance Quiz - 3 Questions
Test your performance knowledge
Which error response format generally improves interaction responsiveness in Express apps?
ASending large images as error responses
BFull HTML pages with stack traces
CMinimal JSON error messages
DRedirecting to another page on error
DevTools: Network
How to check: Open DevTools > Network tab, trigger an error response, select the error request, and check the Response size and Timing waterfall.
What to look for: Look for large response sizes and long blocking times indicating slow error response delivery.