Performance: Response time optimization
This concept affects how quickly the server responds to user requests, impacting the time users wait before seeing any content.
app.get('/data', async (req, res) => { const data = await fastDatabaseQuery(); res.send(data); });
app.get('/data', (req, res) => {
const data = slowDatabaseQuery();
res.send(data);
});| Pattern | Event Loop Blocking | Response Delay | Server Throughput | Verdict |
|---|---|---|---|---|
| Synchronous DB query | High | High | Low | [X] Bad |
| Asynchronous DB query | Low | Low | High | [OK] Good |
| Synchronous file read | High | High | Low | [X] Bad |
| Streaming file response | Low | Low | High | [OK] Good |
| CPU-heavy sync task | High | High | Low | [X] Bad |
| Worker thread offload | Low | Low | High | [OK] Good |
const express = require('express');
const app = express();
app.get('/data', async (req, res) => {
const data = await fetchDataFromDb();
res.send(data);
});
function fetchDataFromDb() {
return new Promise(resolve => setTimeout(() => resolve('DB Data'), 2000));
}
app.listen(3000);const cache = {};
app.get('/user/:id', (req, res) => {
if (cache[req.params.id]) {
res.send(cache[req.params.id]);
return;
}
const userData = getUserFromDb(req.params.id);
cache[req.params.id] = userData;
res.send(userData);
});Pass the test and this pattern moves unknown