Performance: Creating buffers
MEDIUM IMPACT
This concept affects memory allocation speed and CPU usage during buffer creation, impacting server response time and throughput.
const buf = Buffer.alloc(1024);const buf = Buffer.allocUnsafe(1024); buf.fill(0);
| Pattern | Memory Allocation | CPU Usage | Event Loop Impact | Verdict |
|---|---|---|---|---|
| Buffer.allocUnsafe + fill | Allocates uninitialized + manual fill | Higher CPU due to fill | Blocks event loop briefly | [X] Bad |
| Buffer.alloc | Allocates zero-filled memory once | Lower CPU usage | Minimal event loop blocking | [OK] Good |
| Repeated Buffer.from(string) | Many allocations | High CPU and GC pressure | Possible event loop delays | [X] Bad |
| Buffer reuse for common data | Single allocation reused | Low CPU and GC | Smooth event loop | [OK] Good |