Sub topic 6
Sharing a Buffer: byteOffset, length, and Alignment
This chapter assumes you know how to create an ArrayBuffer and a typed array over it, and that you know the byte size of each element type. Here you will place several views at different positions in one buffer and learn the rules the constructor enforces when you do.
The three-argument constructor
Every typed array constructor accepts a buffer, a byteOffset, and a length:
byteOffsetis where the view starts, counted in bytes from the start of the buffer. It defaults to 0.lengthis how many elements the view holds, not how many bytes. If you leave it out, the view runs to the end of the buffer.
People often mix up these two units. The offset is counted in bytes and the length in elements, so a Uint32Array with length 2 covers 8 bytes.
const buf = new ArrayBuffer(8);
const all = new Uint8Array(buf); // bytes 0..7
const tail = new Uint16Array(buf, 2); // bytes 2..7, 3 elements
const mid = new Uint16Array(buf, 2, 2); // bytes 2..5, 2 elements
console.log(all.length, tail.length, mid.length); // 8 3 2
console.log(mid.byteOffset, mid.byteLength); // 2 4Writes are visible through every view
None of these views own any memory. They all read and write the same bytes, so a change made through one view shows up in every view that overlaps it:
const buf = new ArrayBuffer(8);
const all = new Uint8Array(buf);
const mid = new Uint16Array(buf, 2, 2);
all.fill(0xff);
console.log(mid[0].toString(16)); // "ffff"
mid[1] = 0;
console.log(Array.from(all)); // [255, 255, 255, 255, 0, 0, 255, 255]This example sets both bytes of each element to the same value on purpose. When the two bytes differ, which one counts as the "high" byte depends on byte order. A later chapter covers byte order.
The alignment rule
The byteOffset must be a multiple of the element size. A Uint32Array can start at byte 0, 4, or 8, but not at byte 1 or 2. Any other offset throws a RangeError.
const buf = new ArrayBuffer(8);
const ok = new Uint32Array(buf, 4, 1); // byteOffset 4 is a multiple of 4
console.log(ok.byteOffset, ok.length); // 4 1
try {
new Uint32Array(buf, 1);
} catch (e) {
console.log(e instanceof RangeError); // true
}The rule exists because typed arrays map directly onto how the CPU reads memory, and aligned access is fast and simple to implement. For a value that sits at an unaligned position, such as a 32-bit field at byte 3 of a file header, use DataView. It has no alignment requirement.
Common RangeErrors
| Situation | Example | Why it fails |
|---|---|---|
| Misaligned offset | `new Uint16Array(new ArrayBuffer(8), 3)` | 3 is not a multiple of 2 |
| View runs past the end | `new Uint8Array(new ArrayBuffer(8), 6, 4)` | Needs bytes 6 to 9, but the buffer ends at 7 |
| Leftover bytes with no length | `new Uint32Array(new ArrayBuffer(6))` | The remaining 6 bytes are not a whole number of 4-byte elements |
| Offset past the end | `new Uint8Array(new ArrayBuffer(8), 9)` | The start lies outside the buffer |
Exercise
Given const buf = new ArrayBuffer(16), what does new Float64Array(buf, 8).length return, and what happens with new Int16Array(buf, 3)?
Show answer
The Float64Array has length 1, because 8 bytes remain and each element takes 8 bytes. The Int16Array throws a RangeError, because byteOffset 3 is not a multiple of 2.
Exercise
You create const u8 = new Uint8Array(buf) and const u32 = new Uint32Array(buf, 4, 1) over an 8-byte buffer, then run u32[0] = 0. Which indexes of u8 change?
Show answer
Indexes 4 through 7, because the Uint32Array starts at byte 4 and covers 4 bytes. Indexes 0 through 3 do not change.