|
| 1 | +--- |
| 2 | +name: typed-arrays |
| 3 | +description: | |
| 4 | + Modern TypedArray and ArrayBuffer features including resizable buffers, |
| 5 | + transfer operations, Float16Array, and Uint8Array base64/hex encoding. |
| 6 | +compatibility: Node.js 20+ and all the modern browsers |
| 7 | +--- |
| 8 | + |
| 9 | +# Modern Typed Arrays |
| 10 | + |
| 11 | +## ES2023: Change Array by Copy |
| 12 | + |
| 13 | +Immutable operations returning new arrays: |
| 14 | + |
| 15 | +```typescript |
| 16 | +const arr = new Uint8Array([3, 1, 2]); |
| 17 | + |
| 18 | +arr.toReversed(); // Uint8Array [2, 1, 3] |
| 19 | +arr.toSorted((a, b) => a - b); // Uint8Array [1, 2, 3] |
| 20 | +arr.with(0, 99); // Uint8Array [99, 1, 2] |
| 21 | +``` |
| 22 | + |
| 23 | +## ES2023: findLast / findLastIndex |
| 24 | + |
| 25 | +```typescript |
| 26 | +const arr = new Uint8Array([1, 2, 3, 2, 1]); |
| 27 | + |
| 28 | +arr.findLast(x => x === 2); // 2 |
| 29 | +arr.findLastIndex(x => x === 2); // 3 |
| 30 | +``` |
| 31 | + |
| 32 | +## ES2024: Resizable ArrayBuffer |
| 33 | + |
| 34 | +```typescript |
| 35 | +const buffer = new ArrayBuffer(16, { maxByteLength: 1024 }); |
| 36 | + |
| 37 | +buffer.resizable; // true |
| 38 | +buffer.maxByteLength; // 1024 |
| 39 | +buffer.resize(64); // grow |
| 40 | +buffer.resize(8); // shrink |
| 41 | +``` |
| 42 | + |
| 43 | +### Growable SharedArrayBuffer |
| 44 | + |
| 45 | +```typescript |
| 46 | +const shared = new SharedArrayBuffer(16, { maxByteLength: 1024 }); |
| 47 | +shared.growable; // true |
| 48 | +shared.grow(64); // can only grow, not shrink |
| 49 | +``` |
| 50 | + |
| 51 | +### TypedArray tracks resizable buffer |
| 52 | + |
| 53 | +```typescript |
| 54 | +const buffer = new ArrayBuffer(16, { maxByteLength: 64 }); |
| 55 | +const view = new Uint8Array(buffer); |
| 56 | +view.length; // 16 |
| 57 | +buffer.resize(32); |
| 58 | +view.length; // 32 (auto-tracks) |
| 59 | +``` |
| 60 | + |
| 61 | +## ES2024: ArrayBuffer Transfer |
| 62 | + |
| 63 | +```typescript |
| 64 | +const buffer = new ArrayBuffer(16); |
| 65 | +const arr = new Uint8Array(buffer); |
| 66 | +arr[0] = 42; |
| 67 | + |
| 68 | +const newBuffer = buffer.transfer(); // zero-copy transfer |
| 69 | +buffer.detached; // true |
| 70 | +newBuffer.byteLength; // 16 |
| 71 | + |
| 72 | +// Transfer with resize |
| 73 | +const grown = buffer.transfer(64); |
| 74 | + |
| 75 | +// Convert resizable to fixed |
| 76 | +const fixed = resizable.transferToFixedLength(); |
| 77 | +``` |
| 78 | + |
| 79 | +## ES2025: Float16Array |
| 80 | + |
| 81 | +```typescript |
| 82 | +const f16 = new Float16Array(4); |
| 83 | +const f16arr = Float16Array.of(1.5, 2.5, 3.5); |
| 84 | + |
| 85 | +Float16Array.BYTES_PER_ELEMENT; // 2 |
| 86 | +// Range: ±65504 (max), ±6.1e-5 (min positive) |
| 87 | +``` |
| 88 | + |
| 89 | +### DataView Float16 |
| 90 | + |
| 91 | +```typescript |
| 92 | +const view = new DataView(buffer); |
| 93 | +view.setFloat16(0, 3.14, true); // little-endian |
| 94 | +view.getFloat16(0, true); // ≈3.140625 |
| 95 | +``` |
| 96 | + |
| 97 | +## ES2026: Uint8Array Base64 |
| 98 | + |
| 99 | +Not yet in Node.js v24. |
| 100 | + |
| 101 | +### Base64 |
| 102 | + |
| 103 | +```typescript |
| 104 | +const bytes = new Uint8Array([72, 101, 108, 108, 111]); |
| 105 | + |
| 106 | +bytes.toBase64(); // "SGVsbG8=" |
| 107 | +bytes.toBase64({ alphabet: "base64url" }); // URL-safe |
| 108 | +bytes.toBase64({ omitPadding: true }); // no trailing = |
| 109 | + |
| 110 | +Uint8Array.fromBase64("SGVsbG8="); |
| 111 | +Uint8Array.fromBase64("SGVsbG8", { alphabet: "base64url" }); |
| 112 | + |
| 113 | +// Write to existing buffer |
| 114 | +const { read, written } = target.setFromBase64("SGVsbG8="); |
| 115 | +``` |
0 commit comments