Question presented to candidate: "You need to allocate a 4KB buffer in a hot path many times per second. Which Buffer method do you reach for, and what could go wrong?"
What a strong answer should cover:
Buffer.alloc(size)always zero-fills the memory before returning it — this is a hard, spec-guaranteed contract, not just typical behaviour, and it comes with a real, measurable cost for doing that zeroing.Buffer.allocUnsafe(size)skips the zero-fill step entirely for speed. Its contents are unspecified — the Node docs are explicit that this memory may contain old data from a previous allocation, not that it is guaranteed dirty, but also not that it is guaranteed clean.- The realistic danger pattern is not "the whole buffer leaks" — it is a partial write: allocating more bytes than you actually write, then shipping the entire buffer (over a socket, into a file, into a response) including the untouched tail.
Buffer.alloc's tail is always zero;Buffer.allocUnsafe's tail carries no such guarantee. Buffer.from(data)always copies from real, existing source data — a string, an array, another buffer. By construction, there is no uninitialized memory path here at all; it cannot expose old heap contents because it never returns anything except a copy of what you gave it.- The performance difference is real and worth quantifying rather than hand-waved: measured at 4096-byte allocations,
allocUnsafewas consistently ~3.5-4x faster thanallocacross repeated runs on this machine. - The safe use of
allocUnsafeis real and legitimate: when the entire buffer will be immediately and fully overwritten before anything reads or transmits it, there is nothing left to leak — the speed is free in that specific case. The danger only appears when some bytes are left untouched. - Historical context worth citing precisely rather than vaguely: the legacy
new Buffer(size)constructor (pre-datingalloc/allocUnsafe) has long been documented as deprecated and gained a runtimeDeprecationWarning(codeDEP0005) starting in Node 10 — it is the same underlying uninitialized-memory behavior asallocUnsafe, but with no naming to warn a reader that it is the unsafe variant, which is exactly why real CVEs were filed against code that used it carelessly.
Clarifying questions expected:
- "Will every byte of this buffer be overwritten before it is read or sent anywhere?" — the deciding factor for whether
allocUnsafeis safe here. - "Is this genuinely a hot path where the ~3.5-4x allocation cost difference matters?" —
alloc's safety is close to free outside of a real hot path.
Code / implementation expected: Yes — showing the guaranteed-zero tail of alloc versus the unspecified tail of allocUnsafe, plus the measured timing difference, is the concrete deliverable.