0
0
Expressframework~8 mins

Third-party middleware installation in Express - Performance & Optimization

Choose your learning style9 modes available
Performance: Third-party middleware installation
MEDIUM IMPACT
This affects the server response time and the overall latency before the client receives the response.
Adding middleware to handle requests in an Express app
Express
const express = require('express');
const app = express();
const heavyMiddleware = require('heavy-middleware');
app.get('/special', heavyMiddleware(), (req, res) => res.send('Special route'));
app.get('/', (req, res) => res.send('Hello'));
app.listen(3000);
Apply heavy middleware only on routes that need it, reducing unnecessary processing on other requests.
📈 Performance GainReduces average request delay by 50-100ms, improving INP and server throughput.
Adding middleware to handle requests in an Express app
Express
const express = require('express');
const app = express();
const heavyMiddleware = require('heavy-middleware');
app.use(heavyMiddleware());
app.get('/', (req, res) => res.send('Hello'));
app.listen(3000);
Using a heavy middleware globally for all routes causes unnecessary processing on every request, increasing latency.
📉 Performance CostAdds 50-100ms delay per request, increasing INP and slowing server response.
Performance Comparison
PatternMiddleware UsageRequest LatencyServer LoadVerdict
Global heavy middlewareAll routesHigh (50-100ms added)High[X] Bad
Route-specific heavy middlewareOnly needed routesLowModerate[OK] Good
Rendering Pipeline
Middleware runs on the server before sending the response. Heavy or unnecessary middleware increases server processing time, delaying response delivery and affecting user interaction speed.
Server Processing
Response Generation
⚠️ BottleneckServer Processing time due to middleware execution
Core Web Vital Affected
INP
This affects the server response time and the overall latency before the client receives the response.
Optimization Tips
1Avoid installing heavy middleware globally if not needed on all routes.
2Use lightweight middleware and apply it selectively to reduce server latency.
3Monitor server response times to detect middleware bottlenecks early.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of installing heavy third-party middleware globally in Express?
AIt causes layout shifts in the browser
BIt reduces client-side rendering speed
CIt increases server response time for all requests
DIt decreases network bandwidth
DevTools: Network and Performance panels
How to check: Use Network panel to measure server response times; use Performance panel to record server-side processing delays if possible with remote profiling.
What to look for: Look for increased server response time and long blocking periods before response starts, indicating middleware overhead.