0
0
Expressframework~8 mins

res.send for general responses in Express - Performance & Optimization

Choose your learning style9 modes available
Performance: res.send for general responses
MEDIUM IMPACT
This affects server response time and client perceived loading speed by controlling how quickly the server sends data to the browser.
Sending a simple text or JSON response to the client
Express
app.get('/data', async (req, res) => {
  const data = await fetchDataAsync();
  res.json(data);
});
Asynchronous fetching avoids blocking; res.json automatically sets headers and stringifies efficiently.
📈 Performance GainNon-blocking response, reduces server wait time by 50-100ms, faster LCP on client
Sending a simple text or JSON response to the client
Express
app.get('/data', (req, res) => {
  const data = fetchDataSync();
  res.send(JSON.stringify(data));
});
Blocking synchronous data fetching delays response; manual JSON stringification adds overhead.
📉 Performance CostBlocks event loop during fetch, increasing server response time by 50-100ms depending on data size
Performance Comparison
PatternServer BlockingResponse SizeNetwork DelayVerdict
res.send with sync fetch and manual JSONHigh (blocks event loop)Medium (stringified JSON)Medium[X] Bad
res.json with async fetchLow (non-blocking)Medium (automatic JSON)Low[OK] Good
Rendering Pipeline
The server prepares the response and sends it over the network; the browser receives and parses it to render content.
Server Processing
Network Transfer
Browser Parsing
⚠️ BottleneckServer Processing when synchronous/blocking code delays res.send
Core Web Vital Affected
LCP
This affects server response time and client perceived loading speed by controlling how quickly the server sends data to the browser.
Optimization Tips
1Avoid synchronous blocking code before calling res.send or res.json.
2Use res.json() for JSON responses to leverage built-in optimizations.
3Keep response payloads minimal to reduce network transfer time.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a performance benefit of using res.json() over res.send() with manual JSON.stringify()?
Ares.json() blocks the event loop longer
Bres.send() compresses data better
Cres.json() automatically sets headers and stringifies data efficiently
Dres.send() caches the response automatically
DevTools: Network
How to check: Open DevTools > Network tab, reload the page, select the API request, and check the 'Waiting (TTFB)' and 'Content Download' timings.
What to look for: Low Time To First Byte (TTFB) indicates fast server response; large content size or long waiting means slower res.send handling.