Performance: Functional middleware
Functional middleware affects request processing speed and server response time by controlling how requests flow through the middleware stack.
export function fastMiddleware(req, res, next) {
// Non-blocking async operation example
setImmediate(next);
}export function slowMiddleware(req, res, next) {
// Simulate heavy synchronous task
const start = Date.now();
while (Date.now() - start < 100) {}
next();
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Blocking synchronous middleware | N/A (server-side) | N/A | N/A | [X] Bad |
| Non-blocking async middleware | N/A (server-side) | N/A | N/A | [OK] Good |
function logger(req, res, next) {
console.log('Request URL:', req.url);
next();
}function auth(req, res, next) {
if (!req.headers.authorization) {
res.status(401).send('Unauthorized');
}
next();
}Pass the test and this pattern moves unknown