Performance: Middleware concept and execution flow
MEDIUM IMPACT
Middleware affects the server response time and throughput by adding processing steps before sending a response.
app.use(async (req, res, next) => { await heavyAsyncTask(); next(); }); app.use(async (req, res, next) => { await heavyAsyncTask(); next(); });
app.use((req, res, next) => { heavySyncTask(); next(); });
app.use((req, res, next) => { heavySyncTask(); next(); });| Pattern | Event Loop Blocking | Response Delay | Throughput Impact | Verdict |
|---|---|---|---|---|
| Synchronous heavy middleware | Blocks event loop | High delay | Reduces throughput | [X] Bad |
| Asynchronous middleware | Non-blocking | Low delay | Maintains throughput | [OK] Good |
| Static files served after auth | Non-blocking but delayed | Medium delay | Moderate throughput | [!] OK |
| Static files served first | Non-blocking | Low delay | High throughput | [OK] Good |