Performance: Why middleware processes requests before handlers
MEDIUM IMPACT
This affects the time it takes for the server to start processing a request and the overall responsiveness of the application.
app.use(async (req, res, next) => { // lightweight async preprocessing await Promise.resolve(); next(); }); app.get('/data', (req, res) => { res.send('Data'); });
app.use((req, res, next) => {
// heavy synchronous processing
for (let i = 0; i < 1e7; i++) {}
next();
});
app.get('/data', (req, res) => {
res.send('Data');
});| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy synchronous middleware | N/A (server-side) | N/A | N/A | [X] Bad |
| Lightweight async middleware | N/A (server-side) | N/A | N/A | [OK] Good |