Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Buffers and Encoding

Node.js Buffer.alloc() method

Reference for Buffer.alloc() in Node.js: allocate a buffer of a given size, zero-filled or pre-filled with a value or string, with errors and safety notes.

Buffer.alloc(size) creates a new buffer of size bytes and fills it with zeros. Because the memory is always initialized, it can never leak data left over from earlier use of that memory. That makes it the safe default whenever you need an empty buffer to fill later.

You can pass a second argument to pre-fill the buffer with a byte, a string or another buffer; the pattern repeats until the buffer is full. If speed matters and you will overwrite every byte yourself, Buffer.allocUnsafe() skips the clearing.

NODE
const zeros = Buffer.alloc(4);        // <Buffer 00 00 00 00>
const dashes = Buffer.alloc(4, '-');  // <Buffer 2d 2d 2d 2d>

Try it

Node.js Zero-filled and pre-filled

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 A fixed-width record

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.alloc(size[, fill[, encoding]])
Name Description
size Number of bytes. A non-negative integer up to buffer.constants.MAX_LENGTH.
fill Optional. A number (reduced to a byte), a string, a Buffer or a Uint8Array. Default 0.
encoding Encoding of fill when it is a string. Default 'utf8'.

Returns: a Buffer of exactly size bytes.

Errors: ERR_INVALID_ARG_TYPE if size is not a number; ERR_OUT_OF_RANGE for a negative or too-large size (a fractional size such as 1.5 is silently truncated); ERR_INVALID_ARG_VALUE if a string fill cannot be encoded (for example invalid hex). An empty string or empty buffer as fill leaves zeros. A failed allocation for a very large size can throw a RangeError for memory.

Usage notes

  • alloc() is slightly slower than allocUnsafe() for large sizes because of the clearing, but only matters in hot loops that allocate many buffers.
  • A number fill is converted to an unsigned byte (0x101 becomes 1).
  • A multi-byte string that does not divide the size evenly is cut off partway, for instance Buffer.alloc(5, 'รฉ'), which may end with half a UTF-8 character.
  • For sizes below half of Buffer.poolSize, alloc() does not use the shared pool; it always gets its own memory, unlike allocUnsafe() and from().
  • Passing a large number from untrusted input is a denial-of-service risk. Validate sizes that come from the network before allocating.
  • Buffer.alloc(0) is valid and returns an empty buffer.