Sub topic 3
Typed Arrays as Views
This chapter assumes you know regular JavaScript arrays, hexadecimal notation, and the previous chapter's point that an ArrayBuffer is a fixed block of bytes you cannot read or write directly. A typed array gives you that access. It is a view: an object that reads the buffer's bytes as numbers of one fixed type.
The working model
A typed array does not hold its own data. It holds a reference to an ArrayBuffer, plus a starting byte position and an element count. When you read view[i], the engine finds the bytes at that position and decodes them using the view's element type. When you write view[i] = x, it encodes x into those same bytes.
Several views can therefore point at one buffer at the same time. A change made through one view shows up in every other view of that buffer.
const buf = new ArrayBuffer(8);
console.log(buf.byteLength); // 8
const bytes = new Uint8Array(buf);
const words = new Uint32Array(buf);
console.log(bytes.length, words.length); // 8 2
words[0] = 0x01020304;
console.log(bytes[0]); // 4 on little-endian platforms (most hardware)The write through words changed four bytes, and bytes sees them. The order those four bytes end up in depends on the platform. A later chapter on byte order covers this.
Ways to construct a typed array
Every typed array constructor accepts the same argument forms. Some forms create a new buffer and others reuse an existing one.
| Call | New buffer? | Result |
|---|---|---|
| `new Uint8Array(4)` | Yes | 4 zeroed elements |
| `new Uint8Array([1, 2, 3])` | Yes | Copies the values from the array or iterable |
| `new Uint8Array(otherTypedArray)` | Yes | Copies and converts each element |
| `new Uint8Array(buffer)` | No | Views the whole buffer |
| `new Uint8Array(buffer, byteOffset, length)` | No | Views part of the buffer |
const fresh = new Uint8Array(4);
console.log(fresh.buffer.byteLength); // 4, a buffer was created for you
const source = new Uint8Array([10, 20, 30, 40]);
const copy = new Uint8Array(source); // copies the data
const alias = new Uint8Array(source.buffer); // shares the data
source[0] = 99;
console.log(copy[0], alias[0]); // 10 99Properties every view exposes
| Property | Meaning |
|---|---|
| `buffer` | The underlying `ArrayBuffer` |
| `byteOffset` | Where the view starts in the buffer, in bytes |
| `byteLength` | How many bytes the view covers |
| `length` | How many elements the view has |
const buf2 = new ArrayBuffer(16);
const part = new Uint16Array(buf2, 4, 3);
console.log(part.byteOffset, part.length, part.byteLength); // 4 3 6
console.log(part.buffer === buf2); // truebyteLength always equals length times the element size. A later chapter explains the rules for valid offsets.
Common errors
- Assuming
lengthcounts bytes. It counts elements. UsebyteLengthwhen you need bytes. - Using
pushorsplice. A typed array has a fixed length, so these methods do not exist on it. - Reading
view.bufferand assuming it covers only the view's data. It is the whole buffer, which may be larger than the view.
Exercise
You run const a = new Uint8Array([1, 2, 3, 4]); const b = new Uint16Array(a.buffer); const c = new Uint8Array(a); and then a[0] = 0; a[1] = 0;. What are b[0] and c[0]?
Show answer
b[0] is 0, because b shares the buffer and both of its bytes are now zero. c[0] is 1, because c was made from a copy of a's values and does not share memory with it.
Exercise
For new Float64Array(new ArrayBuffer(32), 8), what are length and byteLength?
Show answer
The view starts at byte 8 and runs to the end of the buffer, which leaves 24 bytes. That gives byteLength 24 and length 3, since each Float64 element is 8 bytes.