Typed Arrays and ArrayBuffers

As you know from Chapter 7, JavaScript arrays are general-purpose objects with numeric properties and a special length property. Array elements can be any JavaScript value. Arrays can grow or shrink dynamically and can be sparse. JavaScript implementations perform lots of optimizations so that typical uses of JavaScript arrays are very fast. Typed arrays are array-like objects (Array-Like Objects) that differ from regular arrays in some important ways:

  • The elements of a typed array are all numbers. The constructor used to create the typed array determines the type (signed or unsigned integers or floating point) and size (in bits) of the numbers.

  • Typed arrays have a fixed length.

  • The elements of a typed array are always initialized to 0 when the array is created.

There are eight kinds of typed arrays, each with a different element type. You can create them with the following constructors:

ConstructorNumeric type
Int8Array()signed bytes
Uint8Array()unsigned bytes
Int16Array()signed 16-bit short integers
Uint16Array()unsigned 16-bit short integers
Int32Array()signed 32-bit integers
Uint32Array()unsigned 32-bit integers
Float32Array()32-bit floating-point value
Float64Array()64-bit floating-point value: a regular JavaScript number

Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.