Sub topic 4
Choosing an Element Type
This chapter assumes you already know how to create an ArrayBuffer and wrap it in a typed array view. It also assumes you understand the difference between integers and floating-point numbers in JavaScript. Each typed array constructor fixes two things: how many bytes one element takes up, and how those bytes are read as a number. Choosing the type means matching those two things to your data.
The Element Types at a Glance
| Constructor | Bytes per element | Value range or kind |
|---|---|---|
| `Int8Array` | 1 | -128 to 127 |
| `Uint8Array` | 1 | 0 to 255 |
| `Uint8ClampedArray` | 1 | 0 to 255, clamped |
| `Int16Array` | 2 | -32,768 to 32,767 |
| `Uint16Array` | 2 | 0 to 65,535 |
| `Int32Array` | 4 | -2,147,483,648 to 2,147,483,647 |
| `Uint32Array` | 4 | 0 to 4,294,967,295 |
| `Float32Array` | 4 | IEEE 754 single precision |
| `Float64Array` | 8 | IEEE 754 double precision (same as a JS Number) |
| `BigInt64Array` | 8 | signed 64-bit, as BigInt |
| `BigUint64Array` | 8 | unsigned 64-bit, as BigInt |
Newer engines also provide Float16Array, which was added in ES2025 and uses 2 bytes per element. Check your target runtimes before relying on it.
Every constructor exposes its element size as a static property, BYTES_PER_ELEMENT. Each instance has the same property.
const types = [
Int8Array, Uint8Array, Uint8ClampedArray,
Int16Array, Uint16Array, Int32Array, Uint32Array,
Float32Array, Float64Array, BigInt64Array, BigUint64Array,
];
for (const T of types) {
console.log(T.name, T.BYTES_PER_ELEMENT);
}
const samples = new Float64Array(4);
console.log(samples.length, samples.byteLength); // 4 32The length property counts elements and byteLength counts bytes. The two are equal only when each element is one byte.
Integer or Float, Signed or Unsigned
Start with the kind of data:
- Raw bytes. File contents, network payloads, and encoded text are almost always handled as
Uint8Array, which is the general-purpose byte type. - Pixel channels. Canvas
ImageDatausesUint8ClampedArray, because its out-of-range behaviour suits color math. The next chapter covers how that behaviour works. - Counts and indices. Use unsigned types when values can never be negative. The range doubles at the same byte cost.
- Measurements with fractions. Use
Float64Arraywhen you need the same precision as ordinary Numbers. UseFloat32Arraywhen memory or GPU compatibility (WebGL, WebGPU, audio buffers) matters more than precision. - Integers above 2^53. Use the BigInt types, which a later chapter covers.
The Cost of a Smaller Float
Float32Array halves memory use compared with Float64Array, but it keeps only about 7 significant decimal digits. Values are rounded when you store them, and reading them back gives you the rounded value as a full Number.
const f32 = new Float32Array([0.1, 16777217]);
console.log(f32[0]); // 0.10000000149011612
console.log(f32[1]); // 16777216, because 2^24 + 1 cannot be represented
const f64 = new Float64Array([0.1, 16777217]);
console.log(f64[0], f64[1]); // 0.1 16777217A Decision Path
When you read an existing binary format, the format's specification decides the type. Your preferences do not. If a field is described as "uint16", read it with a 16-bit unsigned view.
Exercise
You need to store one million sensor readings. Each reading is a whole number from 0 to 50,000. Which typed array uses the least memory without losing data, and how many bytes does it need?
Show answer
Uint16Array. Its range of 0 to 65,535 covers 50,000, and each element takes 2 bytes, so the array needs 2,000,000 bytes. Int16Array would not work because its maximum is 32,767, and Uint32Array would double the memory.
Exercise
What does new Float32Array([1.1])[0] === 1.1 evaluate to, and why?
Show answer
It evaluates to false. Storing 1.1 rounds it to the nearest single-precision value, about 1.100000023841858, and that rounded value is what you get back when you read the element.