Testing, Performance & Deployment
Node.js Performance Optimization
Node performance work is mostly about not blocking the event loop, not doing avoidable I/O, and not shipping more data than needed. Profile first — the bottleneck is rarely where it feels like it is.
What is Performance Optimization in Node.js?
Node performance work is mostly about not blocking the event loop, not doing avoidable I/O, and not shipping more data than needed. Profile first — the bottleneck is rarely where it feels like it is.
Key points to remember
- Use --prof or clinic.js to profile rather than guessing.
- Cache expensive reads in Redis with a sensible TTL.
- Enable gzip or brotli compression for JSON responses.
- Run one process per core with cluster or PM2.
- Stream large payloads instead of buffering them.
Symptom to cause
| Symptom | Usual cause | Fix |
|---|---|---|
| All requests slow at once | the event loop is blocked | move CPU work to a worker thread |
| One endpoint slow | a missing database index | add an index; check the explain plan |
| Memory grows steadily | an unbounded cache or listener leak | heap snapshot, add limits and cleanup |
| Slow under load only | connection pool too small | raise maxPoolSize, add caching |
| Large responses | no pagination or field selection | paginate and select only needed fields |
Common mistakes with Performance Optimization
- Optimising JavaScript when the real cost is a database round trip.
- Adding caching before fixing an N+1 query pattern.
Node.js Performance Optimization— Interview Questions & FAQs
How do I find what is blocking the Node event loop?+
Measure event loop lag with perf_hooks monitorEventLoopDelay, or run clinic doctor. Both point at the synchronous work — usually a large JSON parse, a sync file read, or a heavy regular expression.
