0
0
Expressframework~8 mins

res.status for status codes in Express - Performance & Optimization

Choose your learning style9 modes available
Performance: res.status for status codes
MEDIUM IMPACT
This affects server response time and perceived page load speed by setting HTTP status codes efficiently.
Setting HTTP status codes in Express responses
Express
res.status(404).send('Not Found');
Explicitly sets status code before sending response, reducing client confusion and retries.
📈 Performance GainImproves server-client communication, reducing unnecessary network overhead.
Setting HTTP status codes in Express responses
Express
res.send('Not Found'); // without setting status code explicitly
Defaults to status 200 OK, misleading client and causing extra error handling.
📉 Performance CostMay cause client retries or extra error processing, indirectly delaying LCP.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
res.send() without status0 (server-side)00[X] Bad
res.status(code).send()0 (server-side)00[OK] Good
Rendering Pipeline
The server sets the HTTP status code before sending the response. This affects how quickly the browser can start rendering or handle errors.
Network
Browser Response Handling
⚠️ BottleneckNetwork round-trip and client error handling
Core Web Vital Affected
LCP
This affects server response time and perceived page load speed by setting HTTP status codes efficiently.
Optimization Tips
1Always set res.status() before sending a response to communicate the correct HTTP status.
2Avoid sending error messages with default 200 status to prevent client confusion and retries.
3Proper status codes improve server-client communication and reduce page load delays.
Performance Quiz - 3 Questions
Test your performance knowledge
Why is it important to use res.status() before sending a response in Express?
AIt automatically compresses the response body.
BIt speeds up the server CPU processing by 50%.
CIt sets the correct HTTP status code, improving client handling and reducing retries.
DIt caches the response on the client side.
DevTools: Network
How to check: Open DevTools, go to Network tab, reload the page, click the request, and check the Status Code in the headers section.
What to look for: Correct HTTP status code (e.g., 404, 500) confirms proper server response; 200 for errors indicates a problem.