Buffer

Node.js Buffer

Module: node:buffer Support: Experimental Docs: Node.js Docs

Type Aliases

  • ArrayBufferLike --- Node.js Buffer
  • BufferInput --- Node.js Buffer

Interface Buffer

Node.js Buffer — a fixed-length sequence of bytes extending Uint8Array. Supports a wide range of encodings and binary read/write operations.

Since: 1.1.0 Docs: Node.js Docs

55 members: 53 supported, 2 undocumented

Static Methods

🟢 alloc(size, fill, encoding)
Allocates a zero-filled Buffer of the given size. Optionally pre-filled with a value.
🟢 allocUnsafe(size)
Allocates an uninitialized Buffer. Faster than alloc() but may contain old data.
🟢 allocUnsafeSlow(size)
Like allocUnsafe but always uses the C malloc path (bypasses pool).
🟢 from(input, encodingOrOffset, length)
Creates a Buffer from a string, array, ArrayBuffer, or another Buffer.
🟢 isBuffer(obj)
Returns true if the given object is a Buffer instance.
🟢 isEncoding(encoding)
Returns true if the given encoding name is supported.
🟢 byteLength(string, encoding)
Returns the byte length of a string when encoded with the given encoding.
🟢 concat(list, totalLength)
Concatenates a list of Buffers into a single Buffer.
🟢 compare(buf1, buf2)
Compares two Buffers lexicographically (for sorting).

Methods

🟢 compare(target, targetStart, targetEnd, sourceStart, sourceEnd)
Compares two Buffers lexicographically (for sorting).
🟢 copy(target, targetStart, sourceStart, sourceEnd)
Copies bytes from this Buffer into target.
🟢 equals(otherBuffer)
Returns true if this Buffer has the same bytes as otherBuffer.
🟢 fill(value, offset, end, encoding)
Allocates a zero-filled Buffer of the given size. Optionally pre-filled with a value.
🟢 includes(value, byteOffset, encoding)
Returns true if value is found anywhere in the Buffer.
🟢 indexOf(value, byteOffset, encoding)
Returns the index of the first occurrence of value, or -1 if not found.
🟢 lastIndexOf(value, byteOffset, encoding)
Returns the index of the last occurrence of value, or -1 if not found.
🟢 slice(start, end)
Returns a new Buffer referencing the same memory over a sub-range.
🟢 subarray(start, end)
Returns a new Buffer that references the same memory over a sub-range.
🟢 write(string, offset, length, encoding)
Writes a string to the Buffer at the given offset using the specified encoding.
🟢 toString(encoding, start, end)
Decodes the Buffer to a string using the given encoding.
🟢 toJSON()
Returns a JSON representation of the Buffer as `{ type: 'Buffer', data: [...] }`.
🟢 readUInt8(offset)
Reads an unsigned 8-bit integer at the given offset.
🟢 readUInt16BE(offset)
Reads an unsigned 16-bit big-endian integer at the given offset.
🟢 readUInt16LE(offset)
Reads an unsigned 16-bit little-endian integer at the given offset.
🟢 readUInt32BE(offset)
Reads an unsigned 32-bit big-endian integer at the given offset.
🟢 readUInt32LE(offset)
Reads an unsigned 32-bit little-endian integer at the given offset.
🟢 readInt8(offset)
Reads a signed 8-bit integer at the given offset.
🟢 readInt16BE(offset)
Reads a signed 16-bit big-endian integer at the given offset.
🟢 readInt16LE(offset)
Reads a signed 16-bit little-endian integer at the given offset.
🟢 readInt32BE(offset)
Reads a signed 32-bit big-endian integer at the given offset.
🟢 readInt32LE(offset)
Reads a signed 32-bit little-endian integer at the given offset.
🟢 readFloatBE(offset)
Reads a 32-bit big-endian float at the given offset.
🟢 readFloatLE(offset)
Reads a 32-bit little-endian float at the given offset.
🟢 readDoubleBE(offset)
Reads a 64-bit big-endian double at the given offset.
🟢 readDoubleLE(offset)
Reads a 64-bit little-endian double at the given offset.
🟢 writeUInt8(value, offset)
Writes an unsigned 8-bit integer at the given offset.
🟢 writeUInt16BE(value, offset)
Writes an unsigned 16-bit big-endian integer at the given offset.
🟢 writeUInt16LE(value, offset)
Writes an unsigned 16-bit little-endian integer at the given offset.
🟢 writeUInt32BE(value, offset)
Writes an unsigned 32-bit big-endian integer at the given offset.
🟢 writeUInt32LE(value, offset)
Writes an unsigned 32-bit little-endian integer at the given offset.
🟢 writeInt8(value, offset)
Writes a signed 8-bit integer at the given offset.
🟢 writeInt16BE(value, offset)
Writes a signed 16-bit big-endian integer at the given offset.
🟢 writeInt16LE(value, offset)
Writes a signed 16-bit little-endian integer at the given offset.
🟢 writeInt32BE(value, offset)
Writes a signed 32-bit big-endian integer at the given offset.
🟢 writeInt32LE(value, offset)
Writes a signed 32-bit little-endian integer at the given offset.
🟢 writeFloatBE(value, offset)
Writes a 32-bit big-endian float at the given offset.
🟢 writeFloatLE(value, offset)
Writes a 32-bit little-endian float at the given offset.
🟢 writeDoubleBE(value, offset)
Writes a 64-bit big-endian double at the given offset.
🟢 writeDoubleLE(value, offset)
Writes a 64-bit little-endian double at the given offset.
🟢 swap16()
Interprets the Buffer as an array of unsigned 16-bit integers and swaps byte order in-place.
🟢 swap32()
Interprets the Buffer as an array of unsigned 32-bit integers and swaps byte order in-place.
🟢 swap64()
Interprets the Buffer as an array of unsigned 64-bit integers and swaps byte order in-place.

Properties

🟢 length
Creates a Buffer from a string, array, ArrayBuffer, or another Buffer.
⚪ buffer
The underlying ArrayBuffer for this Buffer. @compat elide Full
⚪ byteOffset
The byte offset into the underlying ArrayBuffer where this Buffer starts. @compat elide Full

---

Details

allocUnsafe(size)

static allocUnsafe(size: u32): Buffer

static allocUnsafe(size: u32): Buffer

Allocates an uninitialized Buffer. Faster than alloc() but may contain old data.
ParameterTypeDescription
sizeu32Number of bytes to allocate
Returns: Buffer A new uninitialized Buffer

> Known issue: Contents are not zeroed; do not use for security-sensitive allocations

allocUnsafeSlow(size)

static allocUnsafeSlow(size: u32): Buffer

static allocUnsafeSlow(size: u32): Buffer

Like allocUnsafe but always uses the C malloc path (bypasses pool).
ParameterTypeDescription
sizeu32Number of bytes to allocate
Returns: Buffer A new uninitialized Buffer not backed by a shared pool

> Known issue: Contents are not zeroed; do not use for security-sensitive allocations

slice(start, end)

slice(start?: i32, end?: i32): Buffer

slice(start?: i32, end?: i32): Buffer

Returns a new Buffer referencing the same memory over a sub-range.
ParameterTypeDescription
start (optional)i32Start index (default: 0)
end (optional)i32End index exclusive (default: buffer.length)
Returns: Buffer A Buffer view over the slice

> Known issue: Deprecated in favor of subarray(); both share memory with original

swap16()

swap16(): Buffer

swap16(): Buffer

Interprets the Buffer as an array of unsigned 16-bit integers and swaps byte order in-place.

Returns: Buffer This Buffer (for chaining) Throws:
  • RangeError --- If buffer length is not a multiple of 2

swap32()

swap32(): Buffer

swap32(): Buffer

Interprets the Buffer as an array of unsigned 32-bit integers and swaps byte order in-place.

Returns: Buffer This Buffer (for chaining) Throws:
  • RangeError --- If buffer length is not a multiple of 4

swap64()

swap64(): Buffer

swap64(): Buffer

Interprets the Buffer as an array of unsigned 64-bit integers and swaps byte order in-place.

Returns: Buffer This Buffer (for chaining) Throws:
  • RangeError --- If buffer length is not a multiple of 8

---