TypedArray

A TypedArray object describes an array-like view of an underlying binary data buffer. There is no global property named TypedArray, nor is there a directly visible TypedArray constructor.  Instead, there are a number of different global properties, whose values are typed array constructors for specific element types, listed below. On the following pages you will find common properties and methods that can be used with any typed array containing elements of any type.

Syntax

new TypedArray(length);
new TypedArray(typedArray);
new TypedArray(object);
new TypedArray(buffer [, byteOffset [, length]]);

where TypedArray() is one of:

Int8Array();
Uint8Array();
Uint8ClampedArray();
Int16Array();
Uint16Array();
Int32Array();
Uint32Array();
Float32Array();
Float64Array();

Parameters

length
When called with a length argument, an internal array buffer is created in memory of size length multiplied by BYTES_PER_ELEMENT bytes containing 0 value.
typedArray
When called with a typedArray argument, which can be an object of any of the typed array types (such as Int32Array), the typedArray gets copied into a new typed array. Each value in typedArray is converted to the corresponding type of the constructor before being copied into the new array. Then length of the new typedArray object will be same of length of the typedArray argument.
object
When called with an object argument, a new typed array is created as if by the TypedArray.from() method.
buffer, byteOffset, length
When called with a buffer, and optionally a byteOffset and a length argument, a new typed array view is created that views the specified ArrayBuffer. The byteOffset and length parameters specify the memory range that will be exposed by the typed array view.  If both are omitted, all of buffer is viewed; if only length is omitted, the remainder of buffer is viewed.

Description

ECMAScript 2015 defines a TypedArray constructor that serves as the [[Prototype]] of all TypedArray constructors.  This constructor is not directly exposed: there is no global %TypedArray% or TypedArray property.  It is only directly accessible through Object.getPrototypeOf(Int8Array) and similar.  All TypedArrays constructors inherit common properties from the %TypedArray% constructor function.  Additionally, all typed array prototypes (TypedArray.prototype) have %TypedArray%.prototype as their [[Prototype]].

The %TypedArray% constructor on its own is not particularly useful.  Calling it or using it in a new expression will throw a TypeError, except when used during object creation in JS engines that support subclassing.  There are at present no such engines, so %TypedArray% is only useful to polyfill functions or properties onto all TypedArray constructors.

When creating a TypedArray instance (i.e. instance of Int8Array or similar), an array buffer is created internally (if ArrayBuffer object is present as constructor argument then this array buffer is used) in memory and this buffer address is saved as internal property of that instances, and all the methods of %TypedArray%.prototype uses that array buffer address to operate on i.e. set value and get value etc.

Property access

You can reference elements in the array using standard array index syntax (that is, using bracket notation). However, getting or setting indexed properties on typed arrays will not search in the prototype chain for this property, even when the indices are out of bound. Indexed properties will consult the ArrayBuffer and will never look at object properties. You can still use named properties, just like with all objects.

// Setting and getting using standard array syntax
var int16 = new Int16Array(2);
int16[0] = 42;
console.log(int16[0]); // 42

// Indexed properties on prototypes are not consulted (Fx 25)
Int8Array.prototype[20] = 'foo';
(new Int8Array(32))[20]; // 0
// even when out of bound
Int8Array.prototype[20] = 'foo';
(new Int8Array(8))[20]; // undefined
// or with negative integers
Int8Array.prototype[-1] = 'foo';
(new Int8Array(8))[-1]; // undefined

// Named properties are allowed, though (Fx 30)
Int8Array.prototype.foo = 'bar';
(new Int8Array(32)).foo; // "bar"

TypedArray objects

Type Size in bytes Description Web IDL type Equivalent C type
Int8Array 1 8-bit two's complement signed integer byte int8_t
Uint8Array 1 8-bit unsigned integer octet uint8_t
Uint8ClampedArray 1 8-bit unsigned integer (clamped) octet uint8_t
Int16Array 2 16-bit two's complement signed integer short int16_t
Uint16Array 2 16-bit unsigned integer unsigned short uint16_t
Int32Array 4 32-bit two's complement signed integer long int32_t
Uint32Array 4 32-bit unsigned integer unsigned long uint32_t
Float32Array 4 32-bit IEEE floating point number unrestricted float float
Float64Array 8 64-bit IEEE floating point number unrestricted double double

Properties

TypedArray.BYTES_PER_ELEMENT
Returns a number value of the element size for the different typed array objects.
TypedArray.length
Length property whose value is 0.
TypedArray.name
Returns the string value of the constructor name. E.g "Int8Array".
get TypedArray[@@species]
The constructor function that is used to create derived objects.
TypedArray.prototype
Prototype for the TypedArray objects.

Methods

TypedArray.from()
Creates a new typed array from an array-like or iterable object. See also Array.from().
TypedArray.of()
Creates a new typed array with a variable number of arguments. See also Array.of().

TypedArray prototype

All TypedArrays inherit from TypedArray.prototype.

Properties

{{page('en-US/Web/JavaScript/Reference/Global_Objects/TypedArray/prototype','Properties')}}

Methods

{{page('en-US/Web/JavaScript/Reference/Global_Objects/TypedArray/prototype','Methods')}}

Specifications

Specification Status Comment
Typed Array Specification Obsoleto Defined as TypedArray and ArrayBufferView interface with typed array view types. Superseded by ECMAScript 2015.
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'TypedArray Objects' in that specification.
Padrão Initial definition in an ECMA standard. Specified behaviour for indexed and named properties. Specified that new is required.
ECMAScript (ECMA-262)
The definition of 'TypedArray Objects' in that specification.
Padrão em tempo real  

Browser compatibility

Estamos convertendo nossos dados de compatibilidade para o formato JSON. Esta tabela de compatibilidade ainda usa o formato antigo, pois ainda não convertemos os dados que ela contém. Descubra como você pode ajudar!

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 7.0 4.0 (2) 10 11.6 5.1
Indexed properties not consulting prototype (Yes) [1] 25 (25) ? ? ?
Named properties (Yes) 30 (30) ? ? ?
new is required ? 44 (44) ? ? ?
Iterable in constructor ? 52 (52) ? ? ?
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support 4.0 (Yes) 4.0 (2) 10 11.6 4.2 (Yes)
Indexed properties not consulting prototype ? (Yes) [1] 25.0 (25) ? ? ? (Yes) [1]
Named properties ? (Yes) 30.0 (30) ? ? ? ?
new is required ? ? 44.0 (44) ? ? ? ?
Iterable in constructor ? ? 52.0 (52) ? ? ? ?

[1] -1 and similar are not considered as indexed properties and therefore return the value of the prototype property.

Compatibility notes

Starting with ECMAScript 2015, TypedArray constructors require to be constructed with a new operator. Calling a TypedArray constructor as a function without new, will throw a TypeError from now on.

var dv = Int8Array([1, 2, 3]);
// TypeError: calling a builtin Int8Array constructor
// without new is forbidden
var dv = new Int8Array([1, 2, 3]);

See also