0
0
Expressframework~8 mins

GET route handling in Express - Performance & Optimization

Choose your learning style9 modes available
Performance: GET route handling
MEDIUM IMPACT
This affects server response time and how quickly the browser receives content to start rendering.
Serving a simple static response on a GET request
Express
app.get('/data', async (req, res) => {
  // asynchronous processing or cached result
  const result = await getCachedResult();
  res.send(`Result is ${result}`);
});
Non-blocking async code allows server to handle other requests and respond faster.
📈 Performance GainReduces server response delay, improving LCP by hundreds of milliseconds
Serving a simple static response on a GET request
Express
app.get('/data', (req, res) => {
  // heavy synchronous processing
  let result = 0;
  for(let i = 0; i < 1e8; i++) {
    result += i;
  }
  res.send(`Result is ${result}`);
});
Blocking synchronous processing delays response, increasing server response time and slowing page load.
📉 Performance CostBlocks event loop, delaying response by hundreds of milliseconds
Performance Comparison
PatternServer BlockingResponse DelaySecurity RiskVerdict
Synchronous heavy processing in GET routeBlocks event loopHigh delay (100+ ms)Low[X] Bad
Asynchronous or cached response in GET routeNon-blockingLow delay (<10 ms)Low[OK] Good
Unparameterized DB query in GET routeBlocks event loopMedium delay (50+ ms)High[X] Bad
Parameterized async DB query in GET routeNon-blockingLow delay (<20 ms)Low[OK] Good
Rendering Pipeline
GET route handling affects the server's ability to quickly send HTML or data to the browser, which impacts when the browser can start rendering the page.
Server Processing
Network Transfer
Browser Rendering Start
⚠️ BottleneckServer Processing when route handlers block or do heavy synchronous work
Core Web Vital Affected
LCP
This affects server response time and how quickly the browser receives content to start rendering.
Optimization Tips
1Avoid synchronous blocking code in GET route handlers to keep server responsive.
2Use asynchronous calls and caching to speed up GET route responses.
3Parameterize database queries to improve security and performance.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance problem with synchronous heavy processing inside a GET route handler?
AIt blocks the server event loop, delaying all responses
BIt increases client-side rendering time
CIt reduces network bandwidth
DIt causes layout shifts in the browser
DevTools: Network
How to check: Open DevTools, go to Network tab, reload the page, and look at the time for the GET request to complete.
What to look for: Look for long waiting (TTFB) times indicating slow server response; shorter times mean better performance.