Sub topic 2
ArrayBuffer: A Block of Raw Bytes
This chapter assumes you know core JavaScript syntax, how regular arrays behave, and what bits, bytes, and hexadecimal notation mean. Here you will learn what an ArrayBuffer is, how it differs from a regular array, and why you can't read its bytes directly.
The Working Model
An ArrayBuffer is a fixed-length block of raw memory, measured in bytes. It stores bytes and nothing else. It has no element type, no numbers, and no strings. To read or write those bytes, you attach a view. A view is an object, such as a typed array or a DataView, that interprets the bytes in a particular way.
Creating a Buffer
You create a buffer by passing its size in bytes. Every byte starts at zero.
const buf = new ArrayBuffer(16);
console.log(buf.byteLength); // 16
const bytes = new Uint8Array(buf);
console.log(bytes.every((b) => b === 0)); // trueThe size must be a non-negative integer that the engine can allocate. A negative size, or one that is too large, throws a RangeError.
try {
new ArrayBuffer(-1);
} catch (e) {
console.log(e instanceof RangeError); // true
}How It Differs from an Array
| Feature | Regular `Array` | `ArrayBuffer` |
|---|---|---|
| Contents | Any JavaScript values | Raw bytes only |
| Length | Grows and shrinks freely | Fixed at creation (by default) |
| Indexing with `[i]` | Reads elements | Does not touch the bytes |
| Initial contents | Empty or `undefined` slots | All zeros |
| Memory layout | Engine-specific | Contiguous bytes |
The Indexing Trap
The most common mistake is to treat a buffer as if it were an array. Square brackets on an ArrayBuffer read and write ordinary object properties, not bytes.
const buf = new ArrayBuffer(4);
console.log(buf[0]); // undefined
buf[0] = 42; // creates a normal property named "0"
console.log(new Uint8Array(buf)[0]); // 0, the bytes did not changeNo error is raised, so this bug can go unnoticed. Always go through a view.
Copying Part of a Buffer
slice(start, end) returns a new ArrayBuffer that holds a copy of the selected bytes. Changes to the copy do not affect the original.
const original = new ArrayBuffer(8);
new Uint8Array(original).fill(7);
const part = original.slice(2, 6);
console.log(part.byteLength); // 4
new Uint8Array(part)[0] = 99;
console.log(new Uint8Array(original)[2]); // 7, the original is unchangedTelling Buffers and Views Apart
Many APIs accept either a buffer or a view. ArrayBuffer.isView tells you which one you have.
const buf = new ArrayBuffer(8);
console.log(ArrayBuffer.isView(buf)); // false
console.log(ArrayBuffer.isView(new Uint8Array(buf))); // true
console.log(ArrayBuffer.isView(new DataView(buf))); // trueExercise
You run const b = new ArrayBuffer(2); b[1] = 255; console.log(new Uint8Array(b)[1]);. What is printed, and why?
Show answer
It prints 0. b[1] = 255 creates an ordinary property named "1" on the buffer object and leaves its bytes unchanged. Bytes can only be changed through a view such as a Uint8Array.
Exercise
After const a = new ArrayBuffer(4); const c = a.slice(0, 2); new Uint8Array(c)[0] = 5;, what is new Uint8Array(a)[0]?
Show answer
It is 0. slice copies the bytes into a new ArrayBuffer, so writing to c does not change a.