Skip to content

Buffer — bytes in Node, and how it relates to Uint8Array

Level: 201 · for readers who have met bytes in another language

Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.

One line: Buffer is a Uint8Array subclass that breaks two of its habits: buf.slice() shares memory where Uint8Array's slice copies, and Buffer.from("hello").buffer is a shared pool of Buffer.poolSize bytes, not five.

Keywords: Buffer, Buffer.from, Buffer.alloc, Buffer.allocUnsafe, Buffer.poolSize

What the finished page will answer

  • What does writing through buf.slice(0, 1) do to buf, and what does the same write through Uint8Array.prototype.slice do?
  • How long is Buffer.from("héllo"), and why does it differ from "héllo".length?
  • What are buf.byteOffset and buf.buffer.byteLength for a five-byte buffer, and what else does new Uint8Array(buf.buffer) see?
  • Why may Buffer.allocUnsafe(10) hold old bytes when Buffer.alloc(10) is all zeros?
  • What does one string become through toString in "utf8", "latin1", "base64" and "hex"?

Examples it will need

  • buffers_slice_shares_memory_js.js — a byte written through buf.slice(), buf.subarray() and Uint8Array.prototype.slice, and whether the original changed each time
  • buffers_pool_and_byteoffset_js.js — Buffer.poolSize, byteOffset and buffer.byteLength for a 5-byte and a 10 KiB buffer, and the length of new Uint8Array(buf.buffer)
  • buffers_encodings_js.js — "héllo" through Buffer.from and toString in utf8, utf16le, latin1, base64 and hex, with each byte length

See also

Sources to start from