Node.js performance · Write code
Awaiting inside a loop
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe interviewer asks
This endpoint takes about 9 seconds for a user with 300 orders. Why? How would you make it fast without overloading the database or the shipping service?
app.get('/me/orders/summary', auth, async (req, res) => {
const orders = await db.query('SELECT id FROM orders WHERE user_id = $1', [req.user.id]);
const result = [];
for (const o of orders.rows) {
const items = await db.query('SELECT * FROM order_items WHERE order_id = $1', [o.id]);
const shipment = await shippingApi.getStatus(o.id); // HTTP call, about 25 ms
result.push({ id: o.id, items: items.rows, shipment });
}
res.json(result);
});
Then
Follow-ups the interviewer asks next
- Roughly where do the 9 seconds go?
- Why not wrap everything in
Promise.all? - How would you limit concurrency without a library?
- One shipment call fails. Should the whole response fail?
- How would an ORM hide this, and how do you spot it?
The answer
What a strong answer covers
- N+1 queries plus sequential awaits: 300 × (about 5 ms DB + 25 ms HTTP) ≈ 9 s.
- Database: one query for all items (
WHERE order_id = ANY($1)or a join), then group in memory; select only the columns you need. - Shipping: a batch endpoint if one exists; otherwise bounded concurrency (p-limit or a small pool of about 10), not
Promise.allover 300 calls, which floods the service and hits its rate limits. Promise.allSettledso one failure doesn't fail everything; a timeout per call; a short cache for shipment status.- Paginate: 300 orders in one response is a lot for a phone.
- ORMs hide N+1 behind lazy loading; spot it with query logging or a query-count check in tests.
The red pen
Common traps
Avoid:Promise.all over hundreds of calls; "add an index" as the only fix.
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voicePractise questions like this out loud
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.