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.
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 TypedArray
s 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 an instance of a TypedArray
(e.g. Int8Array
), an array buffer is created internally in memory or, if an ArrayBuffer
object is given as constructor argument, then this is used instead. The buffer address is saved as an internal property of the instance and all the methods of %TypedArray%.prototype
, i.e. set value and get value etc., operate on that array buffer address.
Type | Value Range | Size in bytes | Description | Web IDL type | Equivalent C type |
---|---|---|---|---|---|
Int8Array |
-128 to 127
| 1 | 8-bit two's complement signed integer | byte | int8_t |
Uint8Array |
0 to 255
| 1 | 8-bit unsigned integer | octet | uint8_t |
Uint8ClampedArray |
0 to 255
| 1 | 8-bit unsigned integer (clamped) | octet | uint8_t |
Int16Array |
-32768 to 32767
| 2 | 16-bit two's complement signed integer | short | int16_t |
Uint16Array |
0 to 65535
| 2 | 16-bit unsigned integer | unsigned short | uint16_t |
Int32Array |
-2147483648 to 2147483647
| 4 | 32-bit two's complement signed integer | long | int32_t |
Uint32Array |
0 to 4294967295
| 4 | 32-bit unsigned integer | unsigned long | uint32_t |
Float32Array |
1.2 ×10-38 to 3.4 ×1038
| 4 | 32-bit IEEE floating point number (7 significant digits e.g., 1.1234567 ) | unrestricted float | float |
Float64Array |
5.0 ×10-324 to 1.8 ×10308
| 8 | 64-bit IEEE floating point number (16 significant digits e.g., 1.123...15 ) | unrestricted double | double |
BigInt64Array |
-263 to 263-1
| 8 | 64-bit two's complement signed integer | bigint | int64_t (signed long long) |
BigUint64Array |
0 to 264-1
| 8 | 64-bit unsigned integer | bigint | uint64_t (unsigned long long) |
This object cannot be instantiated directly. Instead, you create an instance of an array of a particular type, such as a Int8Array
or a BigInt64Array
. These objects all have a common syntax for their constructors:
new TypedArray(); new TypedArray(length); new TypedArray(typedArray); new TypedArray(object); new TypedArray(buffer [, byteOffset [, length]]);
Where TypedArray is a constructor for one of the concrete types.
length
length
argument, an internal array buffer is created in memory, of size length
multiplied by BYTES_PER_ELEMENT
bytes, containing zeros.typedArray
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. The length of the new typed array will be same as the length of the typedArray
argument.object
object
argument, a new typed array is created as if by the TypedArray.from()
method.buffer
, byteOffset
, length
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.TypedArray.BYTES_PER_ELEMENT
TypedArray
objects.TypedArray.name
"Int8Array"
).get TypedArray[@@species]
TypedArray.prototype
TypedArray
objects.TypedArray.from()
TypedArray
from an array-like or iterable object. See also Array.from()
.TypedArray.of()
TypedArray
with a variable number of arguments. See also Array.of()
.TypedArray.prototype.buffer
ArrayBuffer
referenced by the typed array. Fixed at construction time and thus read only.TypedArray.prototype.byteLength
TypedArray.prototype.byteOffset
ArrayBuffer
. Fixed at construction time and thus read only.
TypedArray.prototype.length
TypedArray.prototype.copyWithin()
Array.prototype.copyWithin()
.TypedArray.prototype.entries()
Array Iterator
object that contains the key/value pairs for each index in the array. See also Array.prototype.entries()
.TypedArray.prototype.every()
Array.prototype.every()
.TypedArray.prototype.fill()
Array.prototype.fill()
.TypedArray.prototype.filter()
true
. See also Array.prototype.filter()
.TypedArray.prototype.find()
undefined
if not found. See also Array.prototype.find()
.TypedArray.prototype.findIndex()
-1
if not found. See also Array.prototype.findIndex()
.TypedArray.prototype.forEach()
Array.prototype.forEach()
.TypedArray.prototype.includes()
true
or false
as appropriate. See also Array.prototype.includes()
.TypedArray.prototype.indexOf()
-1
if none is found. See also Array.prototype.indexOf()
.TypedArray.prototype.join()
Array.prototype.join()
.TypedArray.prototype.keys()
Array Iterator
that contains the keys for each index in the array. See also Array.prototype.keys()
.TypedArray.prototype.lastIndexOf()
-1
if none is found. See also Array.prototype.lastIndexOf()
.TypedArray.prototype.map()
Array.prototype.map()
.TypedArray.prototype.reduce()
Array.prototype.reduce()
.TypedArray.prototype.reduceRight()
Array.prototype.reduceRight()
.TypedArray.prototype.reverse()
Array.prototype.reverse()
.TypedArray.prototype.set()
TypedArray.prototype.slice()
Array.prototype.slice()
.TypedArray.prototype.some()
true
if at least one element in this array satisfies the provided testing function. See also Array.prototype.some()
.TypedArray.prototype.sort()
Array.prototype.sort()
.TypedArray.prototype.subarray()
TypedArray
from the given start and end element index.TypedArray.prototype.values()
Array Iterator
object that contains the values for each index in the array. See also Array.prototype.values()
.TypedArray.prototype.toLocaleString()
Array.prototype.toLocaleString()
.TypedArray.prototype.toString()
Array.prototype.toString()
.TypedArray.prototype[@@iterator]()
Array Iterator
object that contains the values for each index in the array.Starting with ECMAScript 2015, TypedArray
constructors must be constructed with the new
operator. Calling a TypedArray
constructor as a function without new
will throw a TypeError
.
var dv = Int8Array([1, 2, 3]); // TypeError: calling a builtin Int8Array constructor // without new is forbidden
var dv = new Int8Array([1, 2, 3]);
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"
Desktop | ||||||
---|---|---|---|---|---|---|
TypedArray |
7 | 12 | 4 | 10 | 11.6 | 5.1 |
BYTES_PER_ELEMENT |
7 | 12 | 4 | 10 | 11.6 | 5.1 |
buffer |
7 | 14 | 4 | 10 | 11.6 | 5.1 |
byteLength |
7 | 14 | 4 | 10 | 11.6 | 5.1 |
byteOffset |
7 | 14 | 4 | 10 | 11.6 | 5.1 |
Constructor without arguments | 7 | 12 | 55 | 10 | 11.6 | 5.1 |
copyWithin |
45 | 14 | 34 | No | 36 | 9.1 |
entries |
45 | 14 | 37 | No | 36 | 9.1 |
every |
45 | 14 | 37 | No | 36 | 9.1 |
fill |
45 | 14 | 37 | No | 36 | 9.1 |
filter |
45 | 14 | 38 | No | No | 9.1 |
find |
45 | 14 | 37 | No | 32 | 9.1 |
findIndex |
45 | 14 | 37 | No | 32 | 9.1 |
forEach |
45 | 14 | 38 | No | 32 | 10 |
from |
45 | 14 | 38 | No | No | 10 |
includes |
47 | 14 | 43 | No | 34 | 10 |
Indexed properties not consulting prototype | 7
|
12
|
25 | 10
|
11.6
|
5.1
|
indexOf |
45 | 14 | 37
|
No | 32 | 9.1 |
Iterable in constructor | 39 | 14 | 52 | No | 26 | 10 |
join |
45 | 14 | 37 | No | 32 | 9.1 |
keys |
38 | 14 | 37 | No | 25 | 10 |
lastIndexOf |
45 | 14 | 37
|
No | 32 | 10 |
length |
7 | 14 | 4 | 10 | 11.6 | 5.1 |
map |
45 | 14 | 38 | No | 32 | 9.1 |
name |
7 | 12 | 4 | 10 | 11.6 | 5.1 |
Named properties | 7 | 12 | 30 | 10 | 11.6 | 5.1 |
TypedArray() without new throws |
7 | 14 | 44 | No | 15 | 5.1 |
of |
45 | 14 | 38 | No | No | 9.1 |
reduce |
45 | 14 | 37 | No | 32 | 10 |
reduceRight |
45 | 14 | 37 | No | 32 | 10 |
reverse |
45 | 14 | 37 | No | 32 | 10 |
set |
7 | 14 | 4 | 10 | 11.6 | 5.1 |
slice |
45 | 14 | 38 | No | 32 | 10 |
some |
45 | 14 | 37 | No | 32 | 10 |
sort |
45 | 14 | 46 | No | 32 | 10 |
subarray |
7 | 14 | 4 | 10 | 11.6 | 5.1 |
toLocaleString |
7 | 12 | 51 | 10 | 11.6 | 5.1 |
toString |
7 | 12 | 51 | 10 | 11.6 | 5.1 |
values |
38 | 14 | 37 | No | 25 | 10 |
@@iterator |
38 | 12 | 36
|
No | 25 | 10 |
@@species |
51 | 13 | 48 | No | 38 | 10 |
Mobile | ||||||
---|---|---|---|---|---|---|
TypedArray |
4 | 18 | 4 | 12 | 4.2 | 1.0 |
BYTES_PER_ELEMENT |
4 | 18 | 4 | 12 | 4.2 | 1.0 |
buffer |
4 | 18 | 4 | 12 | 4.2 | 1.0 |
byteLength |
4 | 18 | 4 | 12 | 4.2 | 1.0 |
byteOffset |
4 | 18 | 4 | 12 | 4.2 | 1.0 |
Constructor without arguments | ≤37 | 18 | 55 | 12 | 5 | 1.0 |
copyWithin |
No | No | 34 | No | 9.3 | No |
entries |
No | 45 | 37 | No | 9.3 | 5.0 |
every |
No | 45 | 37 | No | 9.3 | 5.0 |
fill |
No | 45 | 37 | No | 9.3 | 5.0 |
filter |
No | 45 | 38 | No | 9.3 | 5.0 |
find |
45 | 45 | 37 | 32 | 9.3 | 5.0 |
findIndex |
45 | 45 | 37 | 32 | 9.3 | 5.0 |
forEach |
45 | 45 | 38 | 32 | 10 | 5.0 |
from |
No | No | 38 | No | 10 | No |
includes |
No | 47 | 43 | 34 | 10 | 5.0 |
Indexed properties not consulting prototype | ≤37
|
18
|
25 | 12
|
5
|
1.0
|
indexOf |
No | 45 | 37
|
32 | 9.3 | 5.0 |
Iterable in constructor | 39 | 39 | 52 | 26 | 10 | 4.0 |
join |
45 | 45 | 37 | 32 | 9.3 | 5.0 |
keys |
38 | 38 | 37 | 25 | 10 | 3.0 |
lastIndexOf |
45 | 45 | 37
|
32 | 10 | 5.0 |
length |
4 | 18 | 4 | 12 | 4.2 | 1.0 |
map |
45 | 45 | 38 | 32 | 9.3 | 5.0 |
name |
4 | 18 | 4 | 12 | 4.2 | 1.0 |
Named properties | ≤37 | 18 | 30 | 12 | 5 | 1.0 |
TypedArray() without new throws |
≤37 | 18 | 44 | 14 | 5 | 1.0 |
of |
No | No | 38 | No | 9.3 | No |
reduce |
45 | 45 | 37 | No | 10 | 5.0 |
reduceRight |
45 | 45 | 37 | No | 10 | 5.0 |
reverse |
45 | 45 | 37 | No | 10 | 5.0 |
set |
4 | 18 | 4 | 12 | 4.2 | 1.0 |
slice |
45 | 45 | 38 | 32 | 10 | 5.0 |
some |
45 | 45 | 37 | No | 10 | 5.0 |
sort |
45 | 45 | 46 | 32 | 10 | 5.0 |
subarray |
4 | 18 | 4 | 12 | 4.2 | 1.0 |
toLocaleString |
≤37 | 18 | 51 | 12 | 5 | 1.0 |
toString |
≤37 | 18 | 51 | 12 | 5 | 1.0 |
values |
38 | 38 | 37 | 25 | 10 | 3.0 |
@@iterator |
38 | 38 | 36
|
25 | 10 | 3.0 |
@@species |
51 | 51 | 48 | 41 | 10 | 5.0 |
Server | |
---|---|
TypedArray |
0.10 |
BYTES_PER_ELEMENT |
0.10 |
buffer |
0.10 |
byteLength |
0.10 |
byteOffset |
0.10 |
Constructor without arguments | 0.10 |
copyWithin |
4.0.0 |
entries |
0.12 |
every |
4.0.0 |
fill |
4.0.0 |
filter |
4.0.0 |
find |
4.0.0 |
findIndex |
4.0.0 |
forEach |
4.0.0 |
from |
4.0.0 |
includes |
6.0.0
|
Indexed properties not consulting prototype | 0.10
|
indexOf |
4.0.0 |
Iterable in constructor | 4.0.0 |
join |
4.0.0 |
keys |
0.12 |
lastIndexOf |
4.0.0 |
length |
0.10 |
map |
4.0.0 |
name |
0.10 |
Named properties | 0.10 |
TypedArray() without new throws |
0.12 |
of |
4.0.0 |
reduce |
4.0.0 |
reduceRight |
4.0.0 |
reverse |
4.0.0 |
set |
0.10 |
slice |
4.0.0 |
some |
4.0.0 |
sort |
4.0.0 |
subarray |
0.12 |
toLocaleString |
0.10 |
toString |
0.10 |
values |
0.12 |
@@iterator |
0.12 |
@@species |
6.5.0
|
ArrayBuffer
DataView
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray