0
0
Expressframework~8 mins

Status code conventions in Express - Performance & Optimization

Choose your learning style9 modes available
Performance: Status code conventions
MEDIUM IMPACT
This affects how quickly browsers and clients understand server responses, impacting perceived responsiveness and error handling.
Sending appropriate HTTP status codes for API responses
Express
app.get('/data', (req, res) => {
  res.status(404).send({ error: 'Not found' });
});
Using 404 status clearly signals the resource is missing, so clients can handle it immediately without retries.
📈 Performance GainReduces unnecessary client retries and improves interaction responsiveness (INP).
Sending appropriate HTTP status codes for API responses
Express
app.get('/data', (req, res) => {
  res.status(200).send({ error: 'Not found' });
});
Always sending 200 OK even when there is an error confuses clients and causes extra processing or retries.
📉 Performance CostIncreases client processing time and may cause repeated requests, indirectly increasing server load.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Incorrect status codes (e.g., 200 on error)No direct DOM impactNo reflowsNo paint cost[!] OK but causes client delays
Correct status codes (e.g., 404, 500)No direct DOM impactNo reflowsNo paint cost[OK] Improves client handling and responsiveness
Rendering Pipeline
Status codes are part of the HTTP response header and affect how browsers and clients process the response before rendering or further requests.
Network
Client Processing
Rendering
⚠️ BottleneckClient Processing stage, where incorrect status codes cause extra retries or delayed error handling.
Core Web Vital Affected
INP
This affects how quickly browsers and clients understand server responses, impacting perceived responsiveness and error handling.
Optimization Tips
1Always use the correct HTTP status code to reflect the response meaning.
2Avoid sending 200 OK for error conditions to prevent client confusion.
3Proper status codes improve client responsiveness and reduce unnecessary retries.
Performance Quiz - 3 Questions
Test your performance knowledge
Why is it important to send a 404 status code when a resource is not found?
AIt makes the server faster at processing requests.
BIt tells the client immediately the resource is missing, avoiding unnecessary retries.
CIt reduces the size of the response body.
DIt improves the visual layout of the page.
DevTools: Network
How to check: Open DevTools, go to Network tab, reload the page or API call, click the request, and check the Status Code in the headers section.
What to look for: Verify the status code matches the response context (e.g., 404 for missing resource, 500 for server error). Incorrect codes indicate potential performance or UX issues.