Performance: Request body transformation
This affects server response time and client perceived latency by adding processing before handling requests.
app.post('/data', express.json(), (req, res) => { const transformed = {}; for (const [key, value] of Object.entries(req.body)) { transformed[key] = String(value).toUpperCase(); } res.send(transformed); });
app.post('/data', (req, res) => { const transformed = JSON.parse(JSON.stringify(req.body)); for (let key in transformed) { transformed[key] = transformed[key].toString().toUpperCase(); } res.send(transformed); });
| Pattern | CPU Usage | Event Loop Blocking | Response Delay | Verdict |
|---|---|---|---|---|
| Redundant JSON parse/stringify | High | High | High (10-50ms) | [X] Bad |
| Direct transformation after express.json() | Low | Low | Low (2-10ms) | [OK] Good |
req.body.age after transformation?
app.use(express.json());
app.post('/user', (req, res) => {
req.body.age = Number(req.body.age);
res.send(req.body);
});
If the client sends JSON {"age": "30"}?app.use(express.json());
app.post('/data', (req, res) => {
req.body.count = parseInt(req.body.count);
if (!req.body.count) {
res.status(400).send('Count required');
}
res.send(req.body);
});Pass the test and this pattern moves unknown