Performance: Method chaining on response
This affects server response construction speed and memory usage during HTTP request handling.
res.status(200).set('Content-Type', 'application/json').send({ message: 'Hello' });
res.status(200); res.set('Content-Type', 'application/json'); res.send({ message: 'Hello' });
| Pattern | Function Calls | CPU Overhead | Memory Usage | Verdict |
|---|---|---|---|---|
| Separate calls | 3 separate calls | Higher due to multiple function invocations | Minimal | [!] OK |
| Method chaining | 1 chained call sequence | Lower due to fewer calls | Minimal | [OK] Good |
res object allow you to do?res allows chaining methods like status(), set(), and send() to write concise code.res object?status() and set() return the res object, allowing chaining before send().send() to send the response and finish the chain.app.get('/', (req, res) => {
res.status(404).set('Content-Type', 'text/plain').send('Not Found');
});res.send('Hello').status(200).set('Content-Type', 'text/html');send() sends the response and ends it, so chaining after it is invalid.status() and set() cannot be called after send() because response is finished.X-Custom set to Success. Which of the following chained methods is correct?json() method.json() or send() won't work because response ends.Pass the test and this pattern moves unknown