Float16Array
The Float16Array typed array represents an array of 16-bit floating point numbers in the platform byte order. If control over byte order is needed, use DataView instead. The contents are initialized to 0 unless initialization data is explicitly provided. Once established, you can reference elements in the array using the object's methods, or using standard array index syntax (that is, using bracket notation).
Float16Array is a subclass of the hidden TypedArray class.
Note: Float16 support is not universal, both in the JavaScript API and the underlying CPU architecture. Using it may result in slower performance on some platforms. It is intended for interacting with highly optimized and performance-sensitive systems such as float-backed canvases, WebGPU, WebGL, and deep learning models including stable diffusion.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 135 | 135 | 129 | 18.2 | 135 | 18.2 | |
| The getFloat16() method of DataView instances reads 2 bytes starting at the specified byte offset of this DataView and interprets them as a 16-bit floating point number. There is no alignment constraint; multi-byte values may be fetched from any offset within bounds. | 135 | 135 | 129 | 18.2 | 135 | 18.2 |
| The setFloat16() method of DataView instances takes a number and stores it as a 16-bit floating point number in the 2 bytes starting at the specified byte offset of this DataView. There is no alignment constraint; multi-byte values may be stored at any offset within bounds. | 135 | 135 | 129 | 18.2 | 135 | 18.2 |
| The Float16Array() constructor creates Float16Array objects. The contents are initialized to 0 unless initialization data is explicitly provided. | 135 | 135 | 129 | 18.2 | 135 | 18.2 |
| The Math.f16round() static method returns the nearest 16-bit half precision float representation of a number. | 135 | 135 | 129 | 18.2 | 135 | 18.2 |
Syntax
const f16 = new Float16Array([1.5, 2.5, 3.5]);
console.log(f16); // Float16Array [1.5, 2.5, 3.5]
// Rounding to half precision using Math.f16round
Math.f16round(1.337); // 1.3369140625 Live demo
Check Float16Array support
Create a Float16Array when the constructor exists.
Inspect byte length
Compare the memory size of Float16Array and Float32Array.
Map over half-precision values
Transform half-precision values while keeping the typed-array structure.
Use cases
-
ML-style buffers
Store model-related numeric data in a more compact format when full float precision is unnecessary.
-
Graphics pipelines
Prepare data for rendering and GPU-adjacent workflows where compact buffers are valuable.
Cautions
- Half precision trades accuracy for size, so verify that the reduced precision is acceptable for the domain.
- Typed array code becomes harder to read quickly; keep buffer ownership and conversion steps explicit.
Accessibility
- No direct accessibility effect, but efficient numeric processing can support smoother interactive visuals.
- Do not use precision-saving optimizations if they degrade the clarity of user-facing values.