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.
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]].
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.
TypedArray objects
| 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 |
-3.4E38 to 3.4E38 and 1.2E-38 is the min positive number | 4 | 32-bit IEEE floating point number (7 significant digits e.g., 1.234567) | unrestricted float | float |
Float64Array |
-1.8E308 to 1.8E308 and 5E-324 is the min positive number | 8 | 64-bit IEEE floating point number (16 significant digits e.g., 1.23456789012345) | unrestricted double | double |
BigInt64Array |
-2^63 to 2^63 - 1
| 8 | 64-bit two's complement signed integer | bigint | int64_t (signed long long) |
BigUint64Array |
0 to 2^64 - 1
| 8 | 64-bit unsigned integer | bigint | uint64_t (unsigned long long) |
Constructor
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) new TypedArray(buffer, byteOffset) new TypedArray(buffer, byteOffset, length)
Where TypedArray is a constructor for one of the concrete types.
Parameters
length-
When called with a
lengthargument, an internal array buffer is created in memory, of sizelengthmultiplied byBYTES_PER_ELEMENTbytes, containing zeros. typedArray-
When called with a
typedArrayargument, which can be an object of any of the typed array types (such asInt32Array), thetypedArraygets copied into a new typed array. Each value intypedArrayis 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 thetypedArrayargument. object-
When called with an
objectargument, a new typed array is created as if by theTypedArray.from()method. -
buffer,byteOffset,length -
When called with a
buffer, and optionally abyteOffsetand alengthargument, a new typed array view is created that views the specifiedArrayBuffer. ThebyteOffsetandlengthparameters specify the memory range that will be exposed by the typed array view. If both are omitted, all ofbufferis viewed; if onlylengthis omitted, the remainder ofbufferis viewed.
Static properties
TypedArray.BYTES_PER_ELEMENT-
Returns a number value of the element size for the different
TypedArrayobjects. TypedArray.name-
Returns the string value of the constructor name (e.g, "
Int8Array"). get TypedArray[@@species]-
The constructor function used to create derived objects.
TypedArray-
Prototype for
TypedArrayobjects.
Static methods
TypedArray.from()-
Creates a new
TypedArrayfrom an array-like or iterable object. See alsoArray.from(). TypedArray.of()-
Creates a new
TypedArraywith a variable number of arguments. See alsoArray.of().
Instance properties
TypedArray.prototype.buffer-
Returns the
ArrayBufferreferenced by the typed array. Fixed at construction time and thus read only. TypedArray.prototype.byteLength-
Returns the length (in bytes) of the typed array. Fixed at construction time and thus read only.
TypedArray.prototype.byteOffset-
Returns the offset (in bytes) of the typed array from the start of its
ArrayBuffer. Fixed at construction time and thus read only. TypedArray.prototype.length-
Returns the number of elements held in the typed array. Fixed at construction time and thus read only.
Instance methods
TypedArray.prototype.at()-
Takes an integer value and returns the item at that index. This method allows for negative integers, which count back from the last item.
TypedArray.prototype.copyWithin()-
Copies a sequence of array elements within the array. See also
Array.prototype.copyWithin(). TypedArray.prototype.entries()-
Returns a new array iterator object that contains the key/value pairs for each index in the array. See also
Array.prototype.entries(). TypedArray.prototype.every()-
Tests whether all elements in the array pass the test provided by a function. See also
Array.prototype.every(). TypedArray.prototype.fill()-
Fills all the elements of an array from a start index to an end index with a static value. See also
Array.prototype.fill(). TypedArray.prototype.filter()-
Creates a new array with all of the elements of this array for which the provided filtering function returns
true. See alsoArray.prototype.filter(). TypedArray.prototype.find()-
Returns the found value in the array, if an element in the array satisfies the provided testing function, or
undefinedif not found. See alsoArray.prototype.find(). TypedArray.prototype.findIndex()-
Returns the found index in the array, if an element in the array satisfies the provided testing function or
-1if not found. See alsoArray.prototype.findIndex(). TypedArray.prototype.forEach()-
Calls a function for each element in the array. See also
Array.prototype.forEach(). TypedArray.prototype.includes()-
Determines whether a typed array includes a certain element, returning
trueorfalseas appropriate. See alsoArray.prototype.includes(). TypedArray.prototype.indexOf()-
Returns the first (least) index of an element within the array equal to the specified value, or
-1if none is found. See alsoArray.prototype.indexOf(). TypedArray.prototype.join()-
Joins all elements of an array into a string. See also
Array.prototype.join(). TypedArray.prototype.keys()-
Returns a new array iterator that contains the keys for each index in the array. See also
Array.prototype.keys(). TypedArray.prototype.lastIndexOf()-
Returns the last (greatest) index of an element within the array equal to the specified value, or
-1if none is found. See alsoArray.prototype.lastIndexOf(). TypedArray.prototype.map()-
Creates a new array with the results of calling a provided function on every element in this array. See also
Array.prototype.map(). TypedArray.prototype.reduce()-
Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value. See also
Array.prototype.reduce(). TypedArray.prototype.reduceRight()-
Apply a function against an accumulator and each value of the array (from right-to-left) as to reduce it to a single value. See also
Array.prototype.reduceRight(). TypedArray.prototype.reverse()-
Reverses the order of the elements of an array — the first becomes the last, and the last becomes the first. See also
Array.prototype.reverse(). TypedArray.prototype.set()-
Stores multiple values in the typed array, reading input values from a specified array.
TypedArray.prototype.slice()-
Extracts a section of an array and returns a new array. See also
Array.prototype.slice(). TypedArray.prototype.some()-
Returns
trueif at least one element in this array satisfies the provided testing function. See alsoArray.prototype.some(). TypedArray.prototype.sort()-
Sorts the elements of an array in place and returns the array. See also
Array.prototype.sort(). TypedArray.prototype.subarray()-
Returns a new
TypedArrayfrom the given start and end element index. TypedArray.prototype.values()-
Returns a new array iterator object that contains the values for each index in the array. See also
Array.prototype.values(). TypedArray.prototype.toLocaleString()-
Returns a localized string representing the array and its elements. See also
Array.prototype.toLocaleString(). TypedArray.prototype.toString()-
Returns a string representing the array and its elements. See also
Array.prototype.toString(). TypedArray.prototype[@@iterator]()-
Returns a new array iterator object that contains the values for each index in the array.
Examples
New is required
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]);
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"
Cannot be frozen
TypedArrays that aren't empty cannot be frozen, as their underlying ArrayBuffer could be mutated through another TypedArray view of the buffer. This would mean that the object would never genuinely be frozen.
const i8 = Int8Array.of(1, 2, 3); Object.freeze(i8); // TypeError: Cannot freeze array buffer views with elements
ByteOffset must be aligned
When constructing a TypedArray as a view onto an ArrayBuffer, the byteOffset argument must be aligned to its element size; in other words, the offset must be a multiple of BYTES_PER_ELEMENT.
const i32 = new Int32Array(new ArrayBuffer(4), 1); // RangeError: start offset of Int32Array should be a multiple of 4
const i32 = new Int32Array(new ArrayBuffer(4), 0);
ByteLength must be aligned
Like the byteOffset parameter, the byteLength property of an ArrayBuffer passed to a TypedArray's constructor must be a multiple of the constructor's BYTES_PER_ELEMENT.
const i32 = new Int32Array(new ArrayBuffer(3)); // RangeError: byte length of Int32Array should be a multiple of 4
const i32 = new Int32Array(new ArrayBuffer(4));
Specifications
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
TypedArray |
7 |
12 |
4 |
10 |
11.6 |
5.1 |
4 |
18 |
4 |
12 |
4.2 |
1.0 |
BYTES_PER_ELEMENT |
7 |
12 |
4 |
10 |
11.6 |
5.1 |
4 |
18 |
4 |
12 |
4.2 |
1.0 |
at |
92 |
92 |
90 |
No |
78 |
No |
92 |
92 |
90 |
No |
No |
16.0 |
buffer |
7 |
14 |
4 |
10 |
11.6 |
5.1 |
4 |
18 |
4 |
12 |
4.2 |
1.0 |
byteLength |
7 |
14 |
4 |
10 |
11.6 |
5.1 |
4 |
18 |
4 |
12 |
4.2 |
1.0 |
byteOffset |
7 |
14 |
4 |
10 |
11.6 |
5.1 |
4 |
18 |
4 |
12 |
4.2 |
1.0 |
constructor_without_parameters |
7 |
12 |
55 |
10 |
11.6 |
5.1 |
≤37 |
18 |
55 |
12 |
5 |
1.0 |
copyWithin |
45 |
14 |
34 |
No |
36 |
9.1 |
No |
No |
34 |
No |
9.3 |
No |
entries |
45 |
14 |
37 |
No |
36 |
9.1 |
No |
45 |
37 |
No |
9.3 |
5.0 |
every |
45 |
14 |
37 |
No |
36 |
9.1 |
No |
45 |
37 |
No |
9.3 |
5.0 |
fill |
45 |
14 |
37 |
No |
36 |
9.1 |
No |
45 |
37 |
No |
9.3 |
5.0 |
filter |
45 |
14 |
38 |
No |
No |
9.1 |
No |
45 |
38 |
No |
9.3 |
5.0 |
find |
45 |
14 |
37 |
No |
32 |
9.1 |
45 |
45 |
37 |
32 |
9.3 |
5.0 |
findIndex |
45 |
14 |
37 |
No |
32 |
9.1 |
45 |
45 |
37 |
32 |
9.3 |
5.0 |
forEach |
45 |
14 |
38 |
No |
32 |
10 |
45 |
45 |
38 |
32 |
10 |
5.0 |
from |
45 |
14 |
38 |
No |
No |
10 |
No |
No |
38 |
No |
10 |
No |
includes |
47 |
14 |
43 |
No |
34 |
10 |
No |
47 |
43 |
34 |
10 |
5.0 |
index_properties_not_consulting_prototype |
7
Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
|
12
Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
|
25 |
10
Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
|
11.6
Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
|
5.1
Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
|
≤37
Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
|
18
Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
|
25 |
12
Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
|
5
Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
|
1.0
Negative integers are not considered as indexed properties and therefore return the value of the prototype property.
|
indexOf |
45 |
14 |
37
Starting with Firefox 47, this method will no longer return
-0. For example, new Uint8Array([0]).indexOf(0, -0) will now always return +0. |
No |
32 |
9.1 |
No |
45 |
37
Starting with Firefox 47, this method will no longer return
-0. For example, new Uint8Array([0]).indexOf(0, -0) will now always return +0. |
32 |
9.3 |
5.0 |
iterable_in_constructor |
39 |
14 |
52 |
No |
26 |
10 |
39 |
39 |
52 |
26 |
10 |
4.0 |
join |
45 |
14 |
37 |
No |
32 |
9.1 |
45 |
45 |
37 |
32 |
9.3 |
5.0 |
keys |
38 |
14 |
37 |
No |
25 |
10 |
38 |
38 |
37 |
25 |
10 |
3.0 |
lastIndexOf |
45 |
14 |
37
Starting with Firefox 47, this method will no longer return
-0. For example, new Uint8Array([0]).lastIndexOf(0, -0) will now always return +0. |
No |
32 |
10 |
45 |
45 |
37
Starting with Firefox 47, this method will no longer return
-0. For example, new Uint8Array([0]).lastIndexOf(0, -0) will now always return +0. |
32 |
10 |
5.0 |
length |
7 |
14 |
4 |
10 |
11.6 |
5.1 |
4 |
18 |
4 |
12 |
4.2 |
1.0 |
map |
45 |
14 |
38 |
No |
32 |
9.1 |
45 |
45 |
38 |
32 |
9.3 |
5.0 |
name |
7 |
12 |
4 |
10 |
11.6 |
5.1 |
4 |
18 |
4 |
12 |
4.2 |
1.0 |
named_properties |
7 |
12 |
30 |
10 |
11.6 |
5.1 |
≤37 |
18 |
30 |
12 |
5 |
1.0 |
new_required |
7 |
14 |
44 |
No |
15 |
5.1 |
≤37 |
18 |
44 |
14 |
5 |
1.0 |
of |
45 |
14 |
38 |
No |
No |
9.1 |
No |
No |
38 |
No |
9.3 |
No |
reduce |
45 |
14 |
37 |
No |
32 |
10 |
45 |
45 |
37 |
No |
10 |
5.0 |
reduceRight |
45 |
14 |
37 |
No |
32 |
10 |
45 |
45 |
37 |
No |
10 |
5.0 |
reverse |
45 |
14 |
37 |
No |
32 |
10 |
45 |
45 |
37 |
No |
10 |
5.0 |
set |
7 |
14 |
4 |
10 |
11.6 |
5.1 |
4 |
18 |
4 |
12 |
4.2 |
1.0 |
slice |
45 |
14 |
38 |
No |
32 |
10 |
45 |
45 |
38 |
32 |
10 |
5.0 |
some |
45 |
14 |
37 |
No |
32 |
10 |
45 |
45 |
37 |
No |
10 |
5.0 |
sort |
45 |
14 |
46 |
No |
32 |
10 |
45 |
45 |
46 |
32 |
10 |
5.0 |
subarray |
7 |
14 |
4 |
10 |
11.6 |
5.1 |
4 |
18 |
4 |
12 |
4.2 |
1.0 |
toLocaleString |
7 |
12 |
51 |
10 |
11.6 |
5.1 |
≤37 |
18 |
51 |
12 |
5 |
1.0 |
toString |
7 |
12 |
51 |
10 |
11.6 |
5.1 |
≤37 |
18 |
51 |
12 |
5 |
1.0 |
values |
38 |
14 |
37 |
No |
25 |
10 |
38 |
38 |
37 |
25 |
10 |
3.0 |
@@iterator |
38 |
12 |
36
27-36
A placeholder property named
@@iterator is used.17-27
A placeholder property named
iterator is used. |
No |
25 |
10 |
38 |
38 |
36
27-36
A placeholder property named
@@iterator is used.17-27
A placeholder property named
iterator is used. |
25 |
10 |
3.0 |
@@species |
51 |
13 |
48 |
No |
38 |
10 |
51 |
51 |
48 |
41 |
10 |
5.0 |
See also
- A polyfill of typed arrays is available in
core-js - JavaScript typed arrays
ArrayBufferDataView- TextDecoder — Helper that decode strings from numerical data
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray