1. Using Streams Over Buffers
| Approach | Memory |
| Read entire file into Buffer | O(file size) |
| Pipe streams | O(highWaterMark) |
| Use case | Large files, network proxying, transforms |
2. Avoiding Synchronous Operations
Warning: fs.readFileSync, crypto.pbkdf2Sync, large JSON.parse all block the event loop. Never call them in request handlers.
3. Using Worker Threads for CPU-Bound Tasks
| Workload | Recommended |
| Image / PDF processing | Worker pool (piscina) |
| Crypto / hashing | Async crypto APIs first; pool for batch |
| JSON parsing > few MB | Stream parser or worker |
4. Implementing Caching
| Layer | Tool |
| In-process | lru-cache, Map |
| Distributed | Redis / Memcached |
| HTTP | Cache-Control, ETag |
| CDN | CloudFront / Fastly |
5. Using Connection Pooling
| Resource | Library |
| Postgres | pg.Pool |
| MySQL | mysql2/promise pool |
| HTTP | undici.Agent({ connections }) |
6. Optimizing Garbage Collection
| Tip | Effect |
| Reuse buffers | Avoid alloc churn |
| Avoid hidden class transitions | Initialize all properties in constructor |
Tune --max-old-space-size | Match container memory |
| Avoid huge global arrays | Forces major GC |
| Technique | Detail |
| Stream don't buffer | Process row-by-row |
| Use typed arrays | Compact numeric data |
| Drop unused require'd modules | Especially heavy CLIs |
Use node:sea | Single-executable, smaller startup |
8. Using HTTP/2 and HTTP/3
| Protocol | Wins |
| HTTP/2 | Multiplexing, header compression |
| HTTP/3 (QUIC) | UDP-based, no head-of-line blocking v22+ experimental |
9. Compressing Responses (gzip, brotli)
Example: Express
import compression from "compression";
app.use(compression({ threshold: 1024 }));
10. Optimizing JSON Operations
| Tool | Use |
fast-json-stringify | Schema-based serialize |
simdjson bindings | SIMD parsing |
| Stream parsers | stream-json for huge payloads |
11. Using Native Modules When Beneficial
| Example | Why |
sharp | libvips bindings, faster than pure JS |
argon2 / bcrypt | Native CPU-bound work |
| Caveat | Build/install cost, ABI compatibility |
12. Profiling and Benchmarking
| Tool | Use |
autocannon | HTTP benchmark |
wrk / k6 | Load testing |
0x / Clinic flame | Flamegraphs |
tinybench / mitata | Microbenchmarks |