Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Buffers and Encoding

Node.js Buffer.allocUnsafe() method

Reference for Buffer.allocUnsafe() and allocUnsafeSlow() in Node.js: fast uninitialized buffers, the shared pool, security risks and when it is safe to use.

Buffer.allocUnsafe(size) returns a buffer of size bytes without clearing it. The memory may contain whatever was there before: pieces of earlier buffers, strings or secrets. That is what "unsafe" means. It is faster than Buffer.alloc() because it skips the clearing, and for small sizes it takes memory from a shared internal pool, avoiding a fresh allocation entirely.

Use it only when you will overwrite every byte before anything reads the buffer or sends it anywhere: for example when you immediately copy data in with buf.set(), buf.copy() or a full read() into it. If you are unsure, use Buffer.alloc().

NODE
const buf = Buffer.allocUnsafe(4);
buf.fill(0);          // now it is safe to use, same as Buffer.alloc(4)

Try it

Node.js Pool sharing and ownership

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Safe use: fill it completely

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
Buffer.allocUnsafe(size)
Buffer.allocUnsafeSlow(size)
Buffer.poolSize          // number, default size of the internal pool
Name Description
size Number of bytes, a non-negative integer up to buffer.constants.MAX_LENGTH.

Returns: a Buffer of size bytes with unspecified contents.

Errors: ERR_INVALID_ARG_TYPE if size is not a number; ERR_OUT_OF_RANGE for a negative or too-large size.

Usage notes

  • Never return or send an allocUnsafe() buffer that you have not fully overwritten. Leaked bytes from previous allocations have been a real source of security bugs.
  • Sizes of at most half of Buffer.poolSize are carved out of a pre-allocated pool. Several small buffers then share one ArrayBuffer at different byteOffset values, so buf.buffer is bigger than buf.
  • Buffer.allocUnsafeSlow(size) never uses the pool: the buffer gets its own ArrayBuffer. Choose it for long-lived small buffers (such as cached values); otherwise one small retained buffer keeps the whole pool block alive.
  • The pool size is Buffer.poolSize; its value differs between Node.js versions, so do not hard-code it.
  • Command-line flag --zero-fill-buffers makes allocUnsafe() zero its memory, for hardened deployments.
  • Buffer.from() and Buffer.concat() use the same pool for small results, but they write every byte, so they are safe.
  • Prefer alloc() unless profiling shows allocation cost in a hot path.