JavaScript Typed Arrays
JavaScript Typed Arrays
JavaScript Typed Arrays provide a way to handle binary data efficiently. Unlike normal arrays, typed arrays store fixed-size, homogeneous data (all elements of the same type) and are commonly used in graphics, WebGL, and performance-critical tasks.
Why Typed Arrays?
-
Regular arrays can hold any type (numbers, strings, objects), which is flexible but slower.
-
Typed arrays are faster because they store data in contiguous memory with a fixed type.
Types of Typed Arrays
| Typed Array | Description |
|---|---|
Int8Array |
8-bit signed integers |
Uint8Array |
8-bit unsigned integers |
Uint8ClampedArray |
8-bit unsigned integers, clamped to 0–255 |
Int16Array |
16-bit signed integers |
Uint16Array |
16-bit unsigned integers |
Int32Array |
32-bit signed integers |
Uint32Array |
32-bit unsigned integers |
Float32Array |
32-bit floating-point numbers |
Float64Array |
64-bit floating-point numbers |
Creating Typed Arrays
From a length:
From an array:
Accessing and Modifying Elements
Typed Array Properties
| Property | Description |
|---|---|
length |
Number of elements |
buffer |
ArrayBuffer representing the raw binary data |
BYTES_PER_ELEMENT |
Size in bytes of each element |
Iterating Typed Arrays
Converting Typed Array to Regular Array
Practical Uses
-
Graphics and WebGL programming
-
Handling binary files
-
Efficient numeric computations
-
Audio and video processing
Summary Table
| Feature | Description |
|---|---|
| Fixed size | Length cannot be changed after creation |
| Homogeneous | All elements same type |
| Fast | Stored in contiguous memory |
| Common types | Int8Array, Uint8Array, Float32Array, etc. |
| Use cases | Graphics, binary data, performance-critical tasks |
