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 tobuf, and what does the same write throughUint8Array.prototype.slicedo? - How long is
Buffer.from("héllo"), and why does it differ from"héllo".length? - What are
buf.byteOffsetandbuf.buffer.byteLengthfor a five-byte buffer, and what else doesnew Uint8Array(buf.buffer)see? - Why may
Buffer.allocUnsafe(10)hold old bytes whenBuffer.alloc(10)is all zeros? - What does one string become through
toStringin"utf8","latin1","base64"and"hex"?
Examples it will need¶
-
buffers_slice_shares_memory_js.js— a byte written throughbuf.slice(),buf.subarray()andUint8Array.prototype.slice, and whether the original changed each time -
buffers_pool_and_byteoffset_js.js—Buffer.poolSize,byteOffsetandbuffer.byteLengthfor a 5-byte and a 10 KiB buffer, and the length ofnew Uint8Array(buf.buffer) -
buffers_encodings_js.js— "héllo" throughBuffer.fromandtoStringin utf8, utf16le, latin1, base64 and hex, with each byte length
See also¶
- Typed arrays and
ArrayBuffer—Uint8Array,ArrayBufferand views in the language itself - Strings and bytes —
TextEncoder, the standard way to get bytes - Files — where most buffers come from
postMessage— transferring the memory under a buffer to a worker- Python:
bytearrayis the mutable one ↗ — the mutable byte type in Python, beside immutablebytes - Encodings: Encode and decode are verbs ↗ — encode and decode, the two directions
Buffercovers - Encodings: Binary to text ↗ — base64 and hex, which
toStringalso writes