Performance: Why understanding req matters
MEDIUM IMPACT
Understanding the request object affects server response time and resource usage, impacting how fast the server can handle incoming requests.
app.use((req, res, next) => {
const { headers, url, method } = req;
const userAgent = headers['user-agent'];
const authorization = headers['authorization'];
// Use cached variables instead of repeated property access
if (authorization) {
// do something
}
next();
});app.use((req, res, next) => {
const userAgent = req.headers['user-agent'];
const url = req.url;
const method = req.method;
// Accessing req multiple times without caching
if (req.headers['authorization']) {
// do something
}
next();
});| Pattern | CPU Overhead | Memory Usage | Response Time Impact | Verdict |
|---|---|---|---|---|
| Repeated req property access | Higher due to multiple lookups | Minimal | Increases slightly under load | [X] Bad |
| Cached req properties via destructuring | Lower due to single lookup | Minimal | Improves response speed | [OK] Good |