In Node.js 24, how do Buffer.alloc and Buffer.allocUnsafe differ, what is the shared Buffer pool, and when does allocUnsafe leak sensitive data?
answer
- initialised or not
- one slab, many small Buffers
- half of Buffer.poolSize
- bytes sent before being written
- a flag that zero-fills everything
basics
~20 sBuffer.alloc zero-fills and never uses the pool; Buffer.allocUnsafe skips initialisation and carves small sizes from a shared slab of Buffer.poolSize bytes, 64 KiB since Node 24.18.0. Any allocUnsafe byte sent before being overwritten can leak earlier data.
solid answer
~40 s`Buffer.alloc(n)` returns zero-filled memory and never touches the pool. `Buffer.allocUnsafe(n)` returns **uninitialised** memory, and when `n` is under half of `Buffer.poolSize` it is carved from one pre-allocated slab shared with other small Buffers; `Buffer.allocUnsafeSlow(n)` is uninitialised but never pooled. In Node 24.18.0 and later the default `Buffer.poolSize` is 65536, so allocations under 32 KiB are pooled. Uninitialised means the bytes may still hold whatever that memory last held in the process, possibly another request's token, so `allocUnsafe` leaks when any byte goes out unwritten: a 256-byte record where only `out.write(name)` bytes were filled, or a buffer sent whole instead of `buf.subarray(0, bytesWritten)`. Default to `alloc`, use `allocUnsafe` only where the next lines overwrite every byte, and treat `--zero-fill-buffers` as a costly safety net.
code
javascript · 11 linesimport { Buffer } from 'node:buffer';
function encodeName(name) {
const out = Buffer.allocUnsafe(256);
const n = out.write(name, 'utf8'); // bytes actually written
return out.subarray(0, n); // never return `out` itself
}
console.log(encodeName('ada').length); // 3
console.log(Buffer.alloc(4)); // <Buffer 00 00 00 00>
console.log(Buffer.poolSize); // 65536 by default from Node 24.18.0go deeper
Recall that Buffer.alloc zero-fills and Buffer.allocUnsafe does not, and that new Buffer() is deprecated in favour of these explicit calls.
Explain the shared pool: one slab of Buffer.poolSize bytes, used for small allocUnsafe, from and concat results under half the pool size, never for alloc or allocUnsafeSlow.
Spot the leak in review, a partly written allocUnsafe Buffer sent whole, know the 64 KiB default since Node 24.18.0, and judge when allocUnsafeSlow or --zero-fill-buffers is warranted.
Set the team default: Buffer.alloc unless a profile justifies allocUnsafe, a lint rule or review check on every allocUnsafe call, and a decision on whether services handling secrets run with --zero-fill-buffers.
## Three ways to get a Buffer of n bytes | Call | Initialised? | Uses the shared pool? | |---|---|---| | `Buffer.alloc(n[, fill])` | yes: zeros, or `fill` | never | | `Buffer.allocUnsafe(n)` | no | when `n` is under half of `Buffer.poolSize` | | `Buffer.allocUnsafeSlow(n)` | no | never | `Buffer.from(string)`, `Buffer.from(array)` and `Buffer.concat()` also draw small results from the pool, but they write every byte they return, so only the two "unsafe" calls hand you memory that Node has not initialised. ## What "unsafe" means **Uninitialised** means Node does not clear the memory before returning it. The bytes may be whatever that memory last held inside the process: an earlier request body, a header, a session token, key material. That is harmless when the code overwrites every byte before the Buffer is read or sent, and an **information leak** when any part leaves unwritten. The usual shapes: - A fixed-size record is allocated with `Buffer.allocUnsafe(256)`, only `n = out.write(name)` bytes are written, and the whole 256-byte Buffer is sent or stored. - A buffer is filled by a read that returned fewer bytes than requested, and the full buffer is used instead of `buf.subarray(0, bytesRead)`. - Reserved or padding fields in a binary header are skipped instead of being written as zeros. The history explains the API's shape. Before Node 8, `new Buffer(n)` returned uninitialised memory, and the documented attack was real: a JSON field expected to be a string arrived as a number, and `new Buffer(100)` returned 100 bytes of old memory instead of the 3-byte string `"100"`. Node 8 made the constructor zero-fill; the `Buffer()` constructor has been deprecated since Node 6 in favour of the explicit calls above, and since Node 10 it emits a runtime deprecation warning when called from code outside `node_modules`. ## How the pool works When `node:buffer` loads, Node allocates one uninitialised slab of `Buffer.poolSize` bytes. A pooled request takes the next free region and advances an offset, aligned to 8 bytes. When the slab cannot fit the request, Node allocates a fresh slab; the old one stays alive for as long as any Buffer carved from it does. Requests of half `Buffer.poolSize` or more bypass the pool and get their own allocation. - **Who draws from it:** `Buffer.allocUnsafe()`, `Buffer.from(string)`, `Buffer.from(array)` and `Buffer.concat()`; never `Buffer.alloc()` or `Buffer.allocUnsafeSlow()`. - **Why it exists:** thousands of small allocations become cheap offset bumps instead of separate allocations for the garbage collector to track. - **What it costs:** small Buffers share one `ArrayBuffer`, so `buf.buffer` spans unrelated neighbours, and one long-lived small Buffer keeps a whole slab alive. - **What changed in Node 24:** v24.18.0 raised the default `Buffer.poolSize` from 8192 to 65536 bytes, so allocations under 32 KiB are now pooled where the cut-off used to be 4 KiB. `Buffer.poolSize` is writable if a service needs a different trade-off. - **A subtle difference:** `Buffer.allocUnsafe(n).fill(0)` ends with the same contents as `Buffer.alloc(n)`, but for small `n` it comes from the pool, while `Buffer.alloc()` never does. ## Choosing in practice 1. Default to `Buffer.alloc()`. Zero-filling is usually cheap next to the I/O around it. 2. Use `Buffer.allocUnsafe()` in a measured hot path, and only when the next lines overwrite every byte, for example a copy of exactly `n` bytes into a Buffer of `n`. 3. When fewer bytes were written than allocated, send `buf.subarray(0, written)`, never `buf`. 4. Use `Buffer.allocUnsafeSlow()` for small chunks retained long-term, so that each one does not pin a pool slab. 5. In review, treat every `allocUnsafe(` as a claim to be proven: the reviewer should be able to point at the code that writes each byte before the Buffer is read, returned or sent. ## The fleet-wide safety net `node --zero-fill-buffers`, also accepted in `NODE_OPTIONS`, makes newly allocated Buffers start zero-filled, including those from `allocUnsafe()` and `allocUnsafeSlow()`. Node's documentation warns that it can have a measurable negative impact on performance and recommends it only when necessary to guarantee that new Buffers cannot contain old, potentially sensitive data. It is a reasonable stopgap for a service that handles secrets while an audit finds the unsafe call sites. It does not replace fixing them: code that sends unwritten bytes is still wrong, it now sends zeros instead of secrets.
- Is Buffer.allocUnsafe(n).fill(0) equivalent to Buffer.alloc(n)?The contents end up the same, but the memory may not. Node's documentation points out that `allocUnsafe(n).fill(0)` can come from the shared pool when `n` is small, while `Buffer.alloc()` never uses it. A pooled result shares its `ArrayBuffer` with unrelated Buffers, which matters if code later passes `buf.buffer` on or keeps the Buffer for a long time.
- What changed for a service when Node 24.18.0 raised Buffer.poolSize to 64 KiB?Allocations under 32 KiB, rather than under 4 KiB, are now carved from the shared slab, so many more medium-sized Buffers avoid a separate allocation. The flip side is that a long-lived small Buffer can now pin a 64 KiB slab instead of an 8 KiB one, which is visible in heap snapshots of services that retain many small Buffers. `Buffer.poolSize` is writable if a different size suits the workload.
- Buffer.allocUnsafe returns zeroed memory, it is just faster
- Buffer.alloc draws small sizes from the shared pool like allocUnsafe
- allocUnsafe is safe as long as the Buffer is never logged
- new Buffer(n) still returns uninitialised memory in Node 24
- --zero-fill-buffers costs nothing, so every service should set it
Assumes Node.js 24.18.0 or later: that release raised the default Buffer.poolSize from 8192 to 65536 bytes (v26.3.0 on the Current line), moving the pooled cut-off from under 4 KiB to under 32 KiB.