Sub topic 3
Holding Bytes with ArrayBuffer and Uint8Array
This chapter assumes you know modern JavaScript syntax, have a rough idea of what a byte is, and can run code in a browser console or Node.js. Every encoding API later in this tutorial takes or returns a Uint8Array, so you need a clear picture of how that container behaves.
Memory and views
JavaScript keeps raw binary data in two layers:
- An
ArrayBufferis a fixed-length block of memory. You cannot read or write it directly. It only knows its size, which it reports asbyteLength. - A
Uint8Arrayis a view over that memory. It treats each byte as an unsigned integer from 0 to 255, and you index it like an array.
Creating a Uint8Array
You can create a Uint8Array in a few ways:
const zeroed = new Uint8Array(4); // 4 bytes, all 0
const fromList = new Uint8Array([72, 105]); // bytes 72 and 105
const buffer = new ArrayBuffer(8);
const window = new Uint8Array(buffer, 2, 3); // bytes 2..4 of buffer
console.log(zeroed.length); // 4
console.log(fromList[1]); // 105
console.log(window.byteOffset, window.length); // 2 3In a Uint8Array, length and byteLength are always equal because each element is exactly one byte.
Values wrap silently
When you write a value outside 0–255, the Uint8Array does not throw. It truncates the value to an integer and wraps it modulo 256:
const view = new Uint8Array(4);
view[0] = 72;
view[1] = 256; // wraps to 0
view[2] = -1; // wraps to 255
view[3] = 3.9; // truncates to 3
console.log(Array.from(view)); // [72, 0, 255, 3]Shared memory: subarray versus slice
This is where most bugs come from. Two methods look alike but behave differently:
| Method | Returns | Shares memory with the original? |
|---|---|---|
| `subarray(start, end)` | A new view on the same buffer | Yes |
| `slice(start, end)` | A copy in a new buffer | No |
const bytes = new Uint8Array([10, 20, 30, 40, 50]);
const shared = bytes.subarray(1, 3);
const copy = bytes.slice(1, 3);
shared[0] = 99;
console.log(bytes[1]); // 99, the original changed
copy[0] = 1;
console.log(bytes[1]); // 99, the copy is independent
console.log(shared.byteOffset, shared.byteLength); // 1 2
console.log(shared.buffer.byteLength); // 5The last line shows a common mistake. shared.buffer is the whole underlying 5-byte buffer, not just the 2 bytes you selected. If you pass view.buffer to an API, also pass byteOffset and byteLength, or call slice() first to get a buffer that holds only those bytes.
Common errors
- Treating it as a regular array.
Uint8Arrayhas nopushand its length is fixed. Allocate the size you need up front, or build a normal array and convert it once. - Serializing with JSON.
JSON.stringify(new Uint8Array([1, 2]))produces{"0":1,"1":2}, not[1,2]. UseArray.from(bytes)first, or better, base64 (covered later). - Expecting a copy. Data you received through
subarraycan change if something else writes to the shared buffer. Useslicewhen you need to own the bytes. - Comparing with
===. Two arrays with identical bytes are still different objects. Compare lengths and then each element.
Exercise
Given const a = new Uint8Array([1, 2, 3, 4]); const b = a.subarray(2); b[0] = 300;, what does Array.from(a) print, and what is b.buffer.byteLength?
Show answer
[1, 2, 44, 4]. The view b shares memory with a starting at offset 2, and 300 wraps to 300 − 256 = 44. b.buffer.byteLength is 4 because b.buffer is the whole original buffer.
Exercise
Write a function bytesEqual(x, y) that returns true when two Uint8Arrays contain the same bytes.
Show answer
function bytesEqual(x, y) { if (x.length !== y.length) return false; for (let i = 0; i < x.length; i++) { if (x[i] !== y[i]) return false; } return true; }