Sub topic 7
Typed Array Methods: subarray, slice, set, and from
This chapter assumes you know how to create a typed array over an ArrayBuffer, and that you know how integer types wrap values that are out of range. Typed arrays have most of the methods of regular arrays. Some behave differently, and a few are missing. The most important difference to learn is which methods share memory with the original and which methods copy it.
subarray Shares, slice Copies
subarray(start, end) returns a new typed array that views the same buffer. No bytes are copied, so a write through either view shows up in both. slice(start, end) allocates a new buffer and copies the selected elements into it.
const a = new Uint8Array([1, 2, 3, 4]);
const view = a.subarray(1, 3);
view[0] = 99;
console.log(a[1]); // 99
console.log(view.buffer === a.buffer); // true
console.log(view.byteOffset); // 1
const copy = a.slice(1, 3);
copy[0] = 0;
console.log(a[1]); // still 99
console.log(copy.buffer === a.buffer); // falseset: Bulk Copy Into an Existing Array
set(source, offset) copies the elements of a regular array or typed array into the target, starting at offset. It never changes the target's length. If the source would run past the end, set throws a RangeError.
const target = new Uint8Array(6);
target.set([1, 2, 3]);
target.set(new Uint8Array([9, 9]), 4);
console.log(target); // Uint8Array [1, 2, 3, 0, 9, 9]
try {
target.set([1, 2, 3], 5);
} catch (e) {
console.log(e instanceof RangeError); // true
}set is the usual way to join several byte chunks. Allocate one array of the total length, then call set once per chunk, increasing the offset each time.
from and of: Building Typed Arrays
The static methods TypedArray.from(iterable, mapFn) and TypedArray.of(...values) build a new typed array with its own buffer. Each value is converted to the element type as it is stored.
console.log(Uint16Array.from([1, 2, 3], x => x * 1000)); // Uint16Array [1000, 2000, 3000]
console.log(Float32Array.of(1.5, 2.5)); // Float32Array [1.5, 2.5]
console.log(Array.from(new Uint8Array([7, 8]))); // [7, 8], a regular ArrayDifferences from Array
| Behavior | Array | Typed array |
|---|---|---|
| `push`, `pop`, `splice`, `concat` | Available | Not defined, because the length is fixed |
| `map` result | Array of any values | Same typed array type, so values are converted |
| `sort()` default | Compares values as strings | Compares values as numbers |
| `Array.isArray(x)` | `true` | `false` |
const small = new Uint8Array([100, 200]);
console.log(small.map(x => x * 2)); // Uint8Array [200, 144], because 400 wraps
console.log(typeof small.push); // "undefined"
console.log(new Uint8Array([10, 9, 1]).sort()); // Uint8Array [1, 9, 10]
console.log([10, 9, 1].sort()); // [1, 10, 9]If map needs to produce values that the element type cannot hold, convert first with Array.from(typed, fn).
Exercise
Given const a = new Uint8Array([5, 6, 7, 8]); const b = a.subarray(2); const c = a.slice(0, 2); b[0] = 50; c[0] = 0;, what does a contain?
Show answer
Uint8Array [5, 6, 50, 8]. b shares memory with a, so b[0] is a[2]. c is a copy, so the write to c does not change a.
Exercise
You have two Uint8Array chunks, x of length 3 and y of length 5. Write code that joins them into one Uint8Array.
Show answer
const out = new Uint8Array(x.length + y.length); out.set(x, 0); out.set(y, x.length);