0
0
Expressframework~8 mins

Controller pattern for route handlers in Express - Performance & Optimization

Choose your learning style9 modes available
Performance: Controller pattern for route handlers
MEDIUM IMPACT
This pattern affects server response time and how quickly the server can handle incoming requests, indirectly influencing perceived page load speed.
Handling HTTP requests in an Express app
Express
const userController = {
  listActiveUsers: (req, res) => {
    const users = database.getUsers();
    const filtered = users.filter(u => u.active);
    res.json(filtered);
  }
};
app.get('/users', userController.listActiveUsers);
Separates concerns by moving logic to controller functions, enabling reuse, easier testing, and potentially caching or optimization.
📈 Performance GainReduces duplicated CPU work and improves maintainability, leading to faster response times under load
Handling HTTP requests in an Express app
Express
app.get('/users', (req, res) => {
  // complex logic directly in route
  const users = database.getUsers();
  const filtered = users.filter(u => u.active);
  res.json(filtered);
});
All logic is inside the route handler, making it harder to maintain and potentially causing duplicated code and slower response times due to lack of reuse.
📉 Performance CostIncreases server CPU usage and response time under load due to repeated inline logic
Performance Comparison
PatternServer CPU UsageCode MaintainabilityResponse TimeVerdict
Inline route logicHigh due to repeated codeLow, hard to maintainSlower under load[X] Bad
Controller patternLower with reuse and cachingHigh, easier to maintainFaster and scalable[OK] Good
Rendering Pipeline
The controller pattern organizes server-side logic before sending responses, affecting how quickly the server can prepare data for the client. This impacts the time until the browser receives content to render.
Server Processing
Network Transfer
Browser Rendering
⚠️ BottleneckServer Processing due to complex inline logic in routes
Core Web Vital Affected
LCP
This pattern affects server response time and how quickly the server can handle incoming requests, indirectly influencing perceived page load speed.
Optimization Tips
1Keep route handlers simple by moving logic to controllers.
2Reuse controller functions to avoid duplicated CPU work.
3Optimize controller code to reduce server response time and improve LCP.
Performance Quiz - 3 Questions
Test your performance knowledge
How does using the controller pattern for route handlers affect server response time?
AIt has no effect on response time
BIt can reduce response time by organizing and reusing logic
CIt always increases response time due to extra function calls
DIt blocks the browser rendering process
DevTools: Network and Performance panels
How to check: Use Network panel to measure server response time for routes; use Performance panel to profile server-side code if possible or use external profiling tools.
What to look for: Look for long server response times and CPU spikes indicating inefficient route handling