Skip to main content

Optimizing Performance

1. Using Streams Over Buffers

ApproachMemory
Read entire file into BufferO(file size)
Pipe streamsO(highWaterMark)
Use caseLarge 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

WorkloadRecommended
Image / PDF processingWorker pool (piscina)
Crypto / hashingAsync crypto APIs first; pool for batch
JSON parsing > few MBStream parser or worker

4. Implementing Caching

LayerTool
In-processlru-cache, Map
DistributedRedis / Memcached
HTTPCache-Control, ETag
CDNCloudFront / Fastly

5. Using Connection Pooling

ResourceLibrary
Postgrespg.Pool
MySQLmysql2/promise pool
HTTPundici.Agent({ connections })

6. Optimizing Garbage Collection

TipEffect
Reuse buffersAvoid alloc churn
Avoid hidden class transitionsInitialize all properties in constructor
Tune --max-old-space-sizeMatch container memory
Avoid huge global arraysForces major GC

7. Reducing Memory Footprint

TechniqueDetail
Stream don't bufferProcess row-by-row
Use typed arraysCompact numeric data
Drop unused require'd modulesEspecially heavy CLIs
Use node:seaSingle-executable, smaller startup

8. Using HTTP/2 and HTTP/3

ProtocolWins
HTTP/2Multiplexing, 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

ToolUse
fast-json-stringifySchema-based serialize
simdjson bindingsSIMD parsing
Stream parsersstream-json for huge payloads

11. Using Native Modules When Beneficial

ExampleWhy
sharplibvips bindings, faster than pure JS
argon2 / bcryptNative CPU-bound work
CaveatBuild/install cost, ABI compatibility

12. Profiling and Benchmarking

ToolUse
autocannonHTTP benchmark
wrk / k6Load testing
0x / Clinic flameFlamegraphs
tinybench / mitataMicrobenchmarks

Was this article helpful?