0
0
Expressframework~8 mins

Conditional requests handling in Express - Performance & Optimization

Choose your learning style9 modes available
Performance: Conditional requests handling
MEDIUM IMPACT
This affects server response time and network load by reducing unnecessary data transfer when content is unchanged.
Serving static or dynamic content efficiently with caching
Express
app.get('/data', (req, res) => {
  const data = getData();
  const etag = generateETag(data);
  res.set('ETag', etag);
  if (req.headers['if-none-match'] === etag) {
    res.status(304).end();
  } else {
    res.send(data);
  }
});
Sends 304 Not Modified when content is unchanged, saving bandwidth and speeding up response.
📈 Performance GainReduces payload size and improves LCP by avoiding full data transfer on cached requests.
Serving static or dynamic content efficiently with caching
Express
app.get('/data', (req, res) => {
  const data = getData();
  res.send(data);
});
Always sends full response regardless of client cache, causing unnecessary data transfer and slower load times.
📉 Performance CostIncreases network payload and delays LCP due to full content transfer every request.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Always send full responseN/A (server-side)N/AHigh network payload delays paint[X] Bad
Use ETag with 304 Not ModifiedN/A (server-side)N/AMinimal network payload speeds paint[OK] Good
Rendering Pipeline
Conditional requests reduce server response size, which speeds up the network transfer stage and allows the browser to render content faster.
Network Transfer
First Contentful Paint
Largest Contentful Paint
⚠️ BottleneckNetwork Transfer due to large payloads
Core Web Vital Affected
LCP
This affects server response time and network load by reducing unnecessary data transfer when content is unchanged.
Optimization Tips
1Always set ETag or Last-Modified headers for cacheable responses.
2Respond with 304 Not Modified when client cache is valid.
3Avoid sending full response if content has not changed to save bandwidth and improve load speed.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main benefit of handling conditional requests with ETag in Express?
AIncreases server CPU usage
BForces full page reload every time
CReduces data sent when content is unchanged
DDisables browser caching
DevTools: Network
How to check: Open DevTools, go to Network tab, reload page, and inspect the response status code and size for your resource.
What to look for: Look for 304 status codes and small response sizes indicating conditional requests worked.