0
0
Expressframework~8 mins

Express application structure - Performance & Optimization

Choose your learning style9 modes available
Performance: Express application structure
MEDIUM IMPACT
This affects the server response time and how quickly the server can handle requests, impacting the time to first byte (TTFB).
Organizing routes and middleware in an Express app
Express
const express = require('express');
const app = express();

const userRouter = express.Router();
userRouter.get('/', (req, res) => {
  res.send('User page');
});

const adminRouter = express.Router();
adminRouter.use((req, res, next) => {
  // heavy middleware only for admin routes
  for(let i=0; i<1000000; i++) {}
  next();
});
adminRouter.get('/', (req, res) => {
  res.send('Admin page');
});

app.use('/user', userRouter);
app.use('/admin', adminRouter);

app.listen(3000);
Heavy middleware runs only on admin routes, reducing unnecessary processing on other routes.
📈 Performance GainReduces average request processing time by avoiding middleware on unrelated routes.
Organizing routes and middleware in an Express app
Express
const express = require('express');
const app = express();

app.use((req, res, next) => {
  // heavy middleware runs on every request
  for(let i=0; i<1000000; i++) {}
  next();
});

app.get('/user', (req, res) => {
  res.send('User page');
});

app.get('/admin', (req, res) => {
  res.send('Admin page');
});

app.listen(3000);
Heavy middleware runs on every request, even when not needed, causing unnecessary delay.
📉 Performance CostBlocks response for all routes, increasing TTFB by 50-100ms depending on middleware complexity.
Performance Comparison
PatternMiddleware RunsRequest DelayServer LoadVerdict
Global heavy middlewareRuns on all requestsIncreases TTFB by 50-100msHigher CPU usage[X] Bad
Scoped middleware per routerRuns only on relevant routesMinimal delay on unrelated routesLower CPU usage[OK] Good
Rendering Pipeline
Express app structure affects how the server processes incoming requests through middleware and route handlers before sending a response.
Middleware execution
Route matching
Response generation
⚠️ BottleneckMiddleware execution when applied globally without route scoping
Optimization Tips
1Avoid running heavy middleware globally; scope it to routes that need it.
2Organize routes using routers to isolate middleware and reduce processing.
3Keep middleware lightweight to minimize server response delays.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a performance benefit of scoping middleware to specific routes in Express?
AMiddleware runs faster on all routes
BMiddleware runs only when needed, reducing unnecessary processing
CMiddleware is cached by the browser
DMiddleware reduces server memory usage
DevTools: Network panel in browser DevTools and server-side profiling tools
How to check: Use Network panel to measure TTFB for different routes; use Node.js profiler or logging to check middleware execution time
What to look for: Long TTFB or high CPU usage on routes without heavy middleware indicates inefficient structure