23.2 TypedArray Objects

TypedArray objects present an array-like view of an underlying binary data buffer (25.1). A TypedArray element type is the underlying binary scalar data type that all elements of a TypedArray instance have. There is a distinct TypedArray constructor, listed in Table 60, for each of the supported element types. Each constructor in Table 60 has a corresponding distinct prototype object.

Table 60: The TypedArray Constructors
Constructor Name and Intrinsic Element Type Element Size Conversion Operation Description
Int8Array
%Int8Array%
Int8 1 ToInt8 8-bit two's complement signed integer
Uint8Array
%Uint8Array%
Uint8 1 ToUint8 8-bit unsigned integer
Uint8ClampedArray
%Uint8ClampedArray%
Uint8C 1 ToUint8Clamp 8-bit unsigned integer (clamped conversion)
Int16Array
%Int16Array%
Int16 2 ToInt16 16-bit two's complement signed integer
Uint16Array
%Uint16Array%
Uint16 2 ToUint16 16-bit unsigned integer
Int32Array
%Int32Array%
Int32 4 ToInt32 32-bit two's complement signed integer
Uint32Array
%Uint32Array%
Uint32 4 ToUint32 32-bit unsigned integer
BigInt64Array
%BigInt64Array%
BigInt64 8 ToBigInt64 64-bit two's complement signed integer
BigUint64Array
%BigUint64Array%
BigUint64 8 ToBigUint64 64-bit unsigned integer
Float32Array
%Float32Array%
Float32 4 32-bit IEEE floating point
Float64Array
%Float64Array%
Float64 8 64-bit IEEE floating point

In the definitions below, references to TypedArray should be replaced with the appropriate constructor name from the above table.

23.2.1 The %TypedArray% Intrinsic Object

The %TypedArray% intrinsic object:

  • is a constructor function object that all of the TypedArray constructor objects inherit from.
  • along with its corresponding prototype object, provides common properties that are inherited by all TypedArray constructors and their instances.
  • does not have a global name or appear as a property of the global object.
  • acts as the abstract superclass of the various TypedArray constructors.
  • will throw an error when invoked, because it is an abstract class constructor. The TypedArray constructors do not perform a super call to it.

23.2.1.1 %TypedArray% ( )

The %TypedArray% constructor performs the following steps:

  1. Throw a TypeError exception.

The "length" property of the %TypedArray% constructor function is +0๐”ฝ.

23.2.2 Properties of the %TypedArray% Intrinsic Object

The %TypedArray% intrinsic object:

  • has a [[Prototype]] internal slot whose value is %Function.prototype%.
  • has a "name" property whose value is "TypedArray".
  • has the following properties:

23.2.2.1 %TypedArray%.from ( source [ , mapfn [ , thisArg ] ] )

When the from method is called with argument source, and optional arguments mapfn and thisArg, the following steps are taken:

  1. Let C be the this value.
  2. If IsConstructor(C) is false, throw a TypeError exception.
  3. If mapfn is undefined, let mapping be false.
  4. Else,
    1. If IsCallable(mapfn) is false, throw a TypeError exception.
    2. Let mapping be true.
  5. Let usingIterator be ? GetMethod(source, @@iterator).
  6. If usingIterator is not undefined, then
    1. Let values be ? IterableToList(source, usingIterator).
    2. Let len be the number of elements in values.
    3. Let targetObj be ? TypedArrayCreate(C, ยซ ๐”ฝ(len) ยป).
    4. Let k be 0.
    5. Repeat, while k < len,
      1. Let Pk be ! ToString(๐”ฝ(k)).
      2. Let kValue be the first element of values and remove that element from values.
      3. If mapping is true, then
        1. Let mappedValue be ? Call(mapfn, thisArg, ยซ kValue, ๐”ฝ(k) ยป).
      4. Else, let mappedValue be kValue.
      5. Perform ? Set(targetObj, Pk, mappedValue, true).
      6. Set k to k + 1.
    6. Assert: values is now an empty List.
    7. Return targetObj.
  7. NOTE: source is not an Iterable so assume it is already an array-like object.
  8. Let arrayLike be ! ToObject(source).
  9. Let len be ? LengthOfArrayLike(arrayLike).
  10. Let targetObj be ? TypedArrayCreate(C, ยซ ๐”ฝ(len) ยป).
  11. Let k be 0.
  12. Repeat, while k < len,
    1. Let Pk be ! ToString(๐”ฝ(k)).
    2. Let kValue be ? Get(arrayLike, Pk).
    3. If mapping is true, then
      1. Let mappedValue be ? Call(mapfn, thisArg, ยซ kValue, ๐”ฝ(k) ยป).
    4. Else, let mappedValue be kValue.
    5. Perform ? Set(targetObj, Pk, mappedValue, true).
    6. Set k to k + 1.
  13. Return targetObj.

23.2.2.2 %TypedArray%.of ( ...items )

When the of method is called with any number of arguments, the following steps are taken:

  1. Let len be the number of elements in items.
  2. Let C be the this value.
  3. If IsConstructor(C) is false, throw a TypeError exception.
  4. Let newObj be ? TypedArrayCreate(C, ยซ ๐”ฝ(len) ยป).
  5. Let k be 0.
  6. Repeat, while k < len,
    1. Let kValue be items[k].
    2. Let Pk be ! ToString(๐”ฝ(k)).
    3. Perform ? Set(newObj, Pk, kValue, true).
    4. Set k to k + 1.
  7. Return newObj.

23.2.2.3 %TypedArray%.prototype

The initial value of %TypedArray%.prototype is the %TypedArray% prototype object.

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

23.2.2.4 get %TypedArray% [ @@species ]

%TypedArray%[@@species] is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Return the this value.

The value of the "name" property of this function is "get [Symbol.species]".

Note

%TypedArray.prototype% methods normally use their this value's constructor to create a derived object. However, a subclass constructor may over-ride that default behaviour by redefining its @@species property.

23.2.3 Properties of the %TypedArray% Prototype Object

The %TypedArray% prototype object:

  • has a [[Prototype]] internal slot whose value is %Object.prototype%.
  • is %TypedArray.prototype%.
  • is an ordinary object.
  • does not have a [[ViewedArrayBuffer]] or any other of the internal slots that are specific to TypedArray instance objects.

23.2.3.1 get %TypedArray%.prototype.buffer

%TypedArray%.prototype.buffer is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Let O be the this value.
  2. Perform ? RequireInternalSlot(O, [[TypedArrayName]]).
  3. Assert: O has a [[ViewedArrayBuffer]] internal slot.
  4. Let buffer be O.[[ViewedArrayBuffer]].
  5. Return buffer.

23.2.3.2 get %TypedArray%.prototype.byteLength

%TypedArray%.prototype.byteLength is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Let O be the this value.
  2. Perform ? RequireInternalSlot(O, [[TypedArrayName]]).
  3. Assert: O has a [[ViewedArrayBuffer]] internal slot.
  4. Let buffer be O.[[ViewedArrayBuffer]].
  5. If IsDetachedBuffer(buffer) is true, return +0๐”ฝ.
  6. Let size be O.[[ByteLength]].
  7. Return ๐”ฝ(size).

23.2.3.3 get %TypedArray%.prototype.byteOffset

%TypedArray%.prototype.byteOffset is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Let O be the this value.
  2. Perform ? RequireInternalSlot(O, [[TypedArrayName]]).
  3. Assert: O has a [[ViewedArrayBuffer]] internal slot.
  4. Let buffer be O.[[ViewedArrayBuffer]].
  5. If IsDetachedBuffer(buffer) is true, return +0๐”ฝ.
  6. Let offset be O.[[ByteOffset]].
  7. Return ๐”ฝ(offset).

23.2.3.4 %TypedArray%.prototype.constructor

The initial value of %TypedArray%.prototype.constructor is the %TypedArray% intrinsic object.

23.2.3.5 %TypedArray%.prototype.copyWithin ( target, start [ , end ] )

The interpretation and use of the arguments of %TypedArray%.prototype.copyWithin are the same as for Array.prototype.copyWithin as defined in 23.1.3.3.

The following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. Let relativeTarget be ? ToIntegerOrInfinity(target).
  5. If relativeTarget is -โˆž, let to be 0.
  6. Else if relativeTarget < 0, let to be max(len + relativeTarget, 0).
  7. Else, let to be min(relativeTarget, len).
  8. Let relativeStart be ? ToIntegerOrInfinity(start).
  9. If relativeStart is -โˆž, let from be 0.
  10. Else if relativeStart < 0, let from be max(len + relativeStart, 0).
  11. Else, let from be min(relativeStart, len).
  12. If end is undefined, let relativeEnd be len; else let relativeEnd be ? ToIntegerOrInfinity(end).
  13. If relativeEnd is -โˆž, let final be 0.
  14. Else if relativeEnd < 0, let final be max(len + relativeEnd, 0).
  15. Else, let final be min(relativeEnd, len).
  16. Let count be min(final - from, len - to).
  17. If count > 0, then
    1. NOTE: The copying must be performed in a manner that preserves the bit-level encoding of the source data.
    2. Let buffer be O.[[ViewedArrayBuffer]].
    3. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
    4. Let typedArrayName be the String value of O.[[TypedArrayName]].
    5. Let elementSize be the Element Size value specified in Table 60 for typedArrayName.
    6. Let byteOffset be O.[[ByteOffset]].
    7. Let toByteIndex be to ร— elementSize + byteOffset.
    8. Let fromByteIndex be from ร— elementSize + byteOffset.
    9. Let countBytes be count ร— elementSize.
    10. If fromByteIndex < toByteIndex and toByteIndex < fromByteIndex + countBytes, then
      1. Let direction be -1.
      2. Set fromByteIndex to fromByteIndex + countBytes - 1.
      3. Set toByteIndex to toByteIndex + countBytes - 1.
    11. Else,
      1. Let direction be 1.
    12. Repeat, while countBytes > 0,
      1. Let value be GetValueFromBuffer(buffer, fromByteIndex, Uint8, true, Unordered).
      2. Perform SetValueInBuffer(buffer, toByteIndex, Uint8, value, true, Unordered).
      3. Set fromByteIndex to fromByteIndex + direction.
      4. Set toByteIndex to toByteIndex + direction.
      5. Set countBytes to countBytes - 1.
  18. Return O.

23.2.3.6 %TypedArray%.prototype.entries ( )

The following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Return CreateArrayIterator(O, key+value).

23.2.3.7 %TypedArray%.prototype.every ( callbackfn [ , thisArg ] )

The interpretation and use of the arguments of %TypedArray%.prototype.every are the same as for Array.prototype.every as defined in 23.1.3.5.

When the every method is called with one or two arguments, the following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. If IsCallable(callbackfn) is false, throw a TypeError exception.
  5. Let k be 0.
  6. Repeat, while k < len,
    1. Let Pk be ! ToString(๐”ฝ(k)).
    2. Let kValue be ! Get(O, Pk).
    3. Let testResult be ! ToBoolean(? Call(callbackfn, thisArg, ยซ kValue, ๐”ฝ(k), O ยป)).
    4. If testResult is false, return false.
    5. Set k to k + 1.
  7. Return true.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

23.2.3.8 %TypedArray%.prototype.fill ( value [ , start [ , end ] ] )

The interpretation and use of the arguments of %TypedArray%.prototype.fill are the same as for Array.prototype.fill as defined in 23.1.3.6.

The following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. If O.[[ContentType]] is BigInt, set value to ? ToBigInt(value).
  5. Otherwise, set value to ? ToNumber(value).
  6. Let relativeStart be ? ToIntegerOrInfinity(start).
  7. If relativeStart is -โˆž, let k be 0.
  8. Else if relativeStart < 0, let k be max(len + relativeStart, 0).
  9. Else, let k be min(relativeStart, len).
  10. If end is undefined, let relativeEnd be len; else let relativeEnd be ? ToIntegerOrInfinity(end).
  11. If relativeEnd is -โˆž, let final be 0.
  12. Else if relativeEnd < 0, let final be max(len + relativeEnd, 0).
  13. Else, let final be min(relativeEnd, len).
  14. If IsDetachedBuffer(O.[[ViewedArrayBuffer]]) is true, throw a TypeError exception.
  15. Repeat, while k < final,
    1. Let Pk be ! ToString(๐”ฝ(k)).
    2. Perform ! Set(O, Pk, value, true).
    3. Set k to k + 1.
  16. Return O.

23.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )

The interpretation and use of the arguments of %TypedArray%.prototype.filter are the same as for Array.prototype.filter as defined in 23.1.3.7.

When the filter method is called with one or two arguments, the following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. If IsCallable(callbackfn) is false, throw a TypeError exception.
  5. Let kept be a new empty List.
  6. Let k be 0.
  7. Let captured be 0.
  8. Repeat, while k < len,
    1. Let Pk be ! ToString(๐”ฝ(k)).
    2. Let kValue be ! Get(O, Pk).
    3. Let selected be ! ToBoolean(? Call(callbackfn, thisArg, ยซ kValue, ๐”ฝ(k), O ยป)).
    4. If selected is true, then
      1. Append kValue to the end of kept.
      2. Set captured to captured + 1.
    5. Set k to k + 1.
  9. Let A be ? TypedArraySpeciesCreate(O, ยซ ๐”ฝ(captured) ยป).
  10. Let n be 0.
  11. For each element e of kept, do
    1. Perform ! Set(A, ! ToString(๐”ฝ(n)), e, true).
    2. Set n to n + 1.
  12. Return A.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

23.2.3.10 %TypedArray%.prototype.find ( predicate [ , thisArg ] )

The interpretation and use of the arguments of %TypedArray%.prototype.find are the same as for Array.prototype.find as defined in 23.1.3.8.

When the find method is called with one or two arguments, the following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. If IsCallable(predicate) is false, throw a TypeError exception.
  5. Let k be 0.
  6. Repeat, while k < len,
    1. Let Pk be ! ToString(๐”ฝ(k)).
    2. Let kValue be ! Get(O, Pk).
    3. Let testResult be ! ToBoolean(? Call(predicate, thisArg, ยซ kValue, ๐”ฝ(k), O ยป)).
    4. If testResult is true, return kValue.
    5. Set k to k + 1.
  7. Return undefined.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

23.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )

The interpretation and use of the arguments of %TypedArray%.prototype.findIndex are the same as for Array.prototype.findIndex as defined in 23.1.3.9.

When the findIndex method is called with one or two arguments, the following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. If IsCallable(predicate) is false, throw a TypeError exception.
  5. Let k be 0.
  6. Repeat, while k < len,
    1. Let Pk be ! ToString(๐”ฝ(k)).
    2. Let kValue be ! Get(O, Pk).
    3. Let testResult be ! ToBoolean(? Call(predicate, thisArg, ยซ kValue, ๐”ฝ(k), O ยป)).
    4. If testResult is true, return ๐”ฝ(k).
    5. Set k to k + 1.
  7. Return -1๐”ฝ.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

23.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )

The interpretation and use of the arguments of %TypedArray%.prototype.forEach are the same as for Array.prototype.forEach as defined in 23.1.3.12.

When the forEach method is called with one or two arguments, the following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. If IsCallable(callbackfn) is false, throw a TypeError exception.
  5. Let k be 0.
  6. Repeat, while k < len,
    1. Let Pk be ! ToString(๐”ฝ(k)).
    2. Let kValue be ! Get(O, Pk).
    3. Perform ? Call(callbackfn, thisArg, ยซ kValue, ๐”ฝ(k), O ยป).
    4. Set k to k + 1.
  7. Return undefined.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

23.2.3.13 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )

The interpretation and use of the arguments of %TypedArray%.prototype.includes are the same as for Array.prototype.includes as defined in 23.1.3.13.

When the includes method is called with one or two arguments, the following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. If len is 0, return false.
  5. Let n be ? ToIntegerOrInfinity(fromIndex).
  6. Assert: If fromIndex is undefined, then n is 0.
  7. If n is +โˆž, return false.
  8. Else if n is -โˆž, set n to 0.
  9. If n โ‰ฅ 0, then
    1. Let k be n.
  10. Else,
    1. Let k be len + n.
    2. If k < 0, set k to 0.
  11. Repeat, while k < len,
    1. Let elementK be ! Get(O, ! ToString(๐”ฝ(k))).
    2. If SameValueZero(searchElement, elementK) is true, return true.
    3. Set k to k + 1.
  12. Return false.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

23.2.3.14 %TypedArray%.prototype.indexOf ( searchElement [ , fromIndex ] )

The interpretation and use of the arguments of %TypedArray%.prototype.indexOf are the same as for Array.prototype.indexOf as defined in 23.1.3.14.

When the indexOf method is called with one or two arguments, the following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. If len is 0, return -1๐”ฝ.
  5. Let n be ? ToIntegerOrInfinity(fromIndex).
  6. Assert: If fromIndex is undefined, then n is 0.
  7. If n is +โˆž, return -1๐”ฝ.
  8. Else if n is -โˆž, set n to 0.
  9. If n โ‰ฅ 0, then
    1. Let k be n.
  10. Else,
    1. Let k be len + n.
    2. If k < 0, set k to 0.
  11. Repeat, while k < len,
    1. Let kPresent be ! HasProperty(O, ! ToString(๐”ฝ(k))).
    2. If kPresent is true, then
      1. Let elementK be ! Get(O, ! ToString(๐”ฝ(k))).
      2. Let same be the result of performing Strict Equality Comparison searchElement === elementK.
      3. If same is true, return ๐”ฝ(k).
    3. Set k to k + 1.
  12. Return -1๐”ฝ.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

23.2.3.15 %TypedArray%.prototype.join ( separator )

The interpretation and use of the arguments of %TypedArray%.prototype.join are the same as for Array.prototype.join as defined in 23.1.3.15.

When the join method is called with one argument separator, the following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. If separator is undefined, let sep be the single-element String ",".
  5. Else, let sep be ? ToString(separator).
  6. Let R be the empty String.
  7. Let k be 0.
  8. Repeat, while k < len,
    1. If k > 0, set R to the string-concatenation of R and sep.
    2. Let element be ! Get(O, ! ToString(๐”ฝ(k))).
    3. If element is undefined, let next be the empty String; otherwise, let next be ! ToString(element).
    4. Set R to the string-concatenation of R and next.
    5. Set k to k + 1.
  9. Return R.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

23.2.3.16 %TypedArray%.prototype.keys ( )

The following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Return CreateArrayIterator(O, key).

23.2.3.17 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] )

The interpretation and use of the arguments of %TypedArray%.prototype.lastIndexOf are the same as for Array.prototype.lastIndexOf as defined in 23.1.3.17.

When the lastIndexOf method is called with one or two arguments, the following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. If len is 0, return -1๐”ฝ.
  5. If fromIndex is present, let n be ? ToIntegerOrInfinity(fromIndex); else let n be len - 1.
  6. If n is -โˆž, return -1๐”ฝ.
  7. If n โ‰ฅ 0, then
    1. Let k be min(n, len - 1).
  8. Else,
    1. Let k be len + n.
  9. Repeat, while k โ‰ฅ 0,
    1. Let kPresent be ! HasProperty(O, ! ToString(๐”ฝ(k))).
    2. If kPresent is true, then
      1. Let elementK be ! Get(O, ! ToString(๐”ฝ(k))).
      2. Let same be the result of performing Strict Equality Comparison searchElement === elementK.
      3. If same is true, return ๐”ฝ(k).
    3. Set k to k - 1.
  10. Return -1๐”ฝ.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

23.2.3.18 get %TypedArray%.prototype.length

%TypedArray%.prototype.length is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Let O be the this value.
  2. Perform ? RequireInternalSlot(O, [[TypedArrayName]]).
  3. Assert: O has [[ViewedArrayBuffer]] and [[ArrayLength]] internal slots.
  4. Let buffer be O.[[ViewedArrayBuffer]].
  5. If IsDetachedBuffer(buffer) is true, return +0๐”ฝ.
  6. Let length be O.[[ArrayLength]].
  7. Return ๐”ฝ(length).

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

23.2.3.19 %TypedArray%.prototype.map ( callbackfn [ , thisArg ] )

The interpretation and use of the arguments of %TypedArray%.prototype.map are the same as for Array.prototype.map as defined in 23.1.3.18.

When the map method is called with one or two arguments, the following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. If IsCallable(callbackfn) is false, throw a TypeError exception.
  5. Let A be ? TypedArraySpeciesCreate(O, ยซ ๐”ฝ(len) ยป).
  6. Let k be 0.
  7. Repeat, while k < len,
    1. Let Pk be ! ToString(๐”ฝ(k)).
    2. Let kValue be ! Get(O, Pk).
    3. Let mappedValue be ? Call(callbackfn, thisArg, ยซ kValue, ๐”ฝ(k), O ยป).
    4. Perform ? Set(A, Pk, mappedValue, true).
    5. Set k to k + 1.
  8. Return A.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

23.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] )

The interpretation and use of the arguments of %TypedArray%.prototype.reduce are the same as for Array.prototype.reduce as defined in 23.1.3.21.

When the reduce method is called with one or two arguments, the following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. If IsCallable(callbackfn) is false, throw a TypeError exception.
  5. If len = 0 and initialValue is not present, throw a TypeError exception.
  6. Let k be 0.
  7. Let accumulator be undefined.
  8. If initialValue is present, then
    1. Set accumulator to initialValue.
  9. Else,
    1. Let Pk be ! ToString(๐”ฝ(k)).
    2. Set accumulator to ! Get(O, Pk).
    3. Set k to k + 1.
  10. Repeat, while k < len,
    1. Let Pk be ! ToString(๐”ฝ(k)).
    2. Let kValue be ! Get(O, Pk).
    3. Set accumulator to ? Call(callbackfn, undefined, ยซ accumulator, kValue, ๐”ฝ(k), O ยป).
    4. Set k to k + 1.
  11. Return accumulator.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

23.2.3.21 %TypedArray%.prototype.reduceRight ( callbackfn [ , initialValue ] )

The interpretation and use of the arguments of %TypedArray%.prototype.reduceRight are the same as for Array.prototype.reduceRight as defined in 23.1.3.22.

When the reduceRight method is called with one or two arguments, the following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. If IsCallable(callbackfn) is false, throw a TypeError exception.
  5. If len is 0 and initialValue is not present, throw a TypeError exception.
  6. Let k be len - 1.
  7. Let accumulator be undefined.
  8. If initialValue is present, then
    1. Set accumulator to initialValue.
  9. Else,
    1. Let Pk be ! ToString(๐”ฝ(k)).
    2. Set accumulator to ! Get(O, Pk).
    3. Set k to k - 1.
  10. Repeat, while k โ‰ฅ 0,
    1. Let Pk be ! ToString(๐”ฝ(k)).
    2. Let kValue be ! Get(O, Pk).
    3. Set accumulator to ? Call(callbackfn, undefined, ยซ accumulator, kValue, ๐”ฝ(k), O ยป).
    4. Set k to k - 1.
  11. Return accumulator.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

23.2.3.22 %TypedArray%.prototype.reverse ( )

The interpretation and use of the arguments of %TypedArray%.prototype.reverse are the same as for Array.prototype.reverse as defined in 23.1.3.23.

When the reverse method is called, the following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. Let middle be floor(len / 2).
  5. Let lower be 0.
  6. Repeat, while lower โ‰  middle,
    1. Let upper be len - lower - 1.
    2. Let upperP be ! ToString(๐”ฝ(upper)).
    3. Let lowerP be ! ToString(๐”ฝ(lower)).
    4. Let lowerValue be ! Get(O, lowerP).
    5. Let upperValue be ! Get(O, upperP).
    6. Perform ! Set(O, lowerP, upperValue, true).
    7. Perform ! Set(O, upperP, lowerValue, true).
    8. Set lower to lower + 1.
  7. Return O.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

23.2.3.23 %TypedArray%.prototype.set ( source [ , offset ] )

%TypedArray%.prototype.set is a function whose behaviour differs based upon the type of its first argument.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

Sets multiple values in this TypedArray, reading the values from source. The optional offset value indicates the first element index in this TypedArray where values are written. If omitted, it is assumed to be 0.

  1. Let target be the this value.
  2. Perform ? RequireInternalSlot(target, [[TypedArrayName]]).
  3. Assert: target has a [[ViewedArrayBuffer]] internal slot.
  4. Let targetOffset be ? ToIntegerOrInfinity(offset).
  5. If targetOffset < 0, throw a RangeError exception.
  6. If source is an Object that has a [[TypedArrayName]] internal slot, then
    1. Perform ? SetTypedArrayFromTypedArray(target, targetOffset, source).
  7. Else,
    1. Perform ? SetTypedArrayFromArrayLike(target, targetOffset, source).
  8. Return undefined.

23.2.3.23.1 SetTypedArrayFromTypedArray ( target, targetOffset, source )

The abstract operation SetTypedArrayFromTypedArray takes arguments target (a TypedArray object), targetOffset (a non-negative integer or +โˆž), and source (a TypedArray object). It sets multiple values in target, starting at index targetOffset, reading the values from source. It performs the following steps when called:

  1. Assert: source is an Object that has a [[TypedArrayName]] internal slot.
  2. Let targetBuffer be target.[[ViewedArrayBuffer]].
  3. If IsDetachedBuffer(targetBuffer) is true, throw a TypeError exception.
  4. Let targetLength be target.[[ArrayLength]].
  5. Let srcBuffer be source.[[ViewedArrayBuffer]].
  6. If IsDetachedBuffer(srcBuffer) is true, throw a TypeError exception.
  7. Let targetName be the String value of target.[[TypedArrayName]].
  8. Let targetType be the Element Type value in Table 60 for targetName.
  9. Let targetElementSize be the Element Size value specified in Table 60 for targetName.
  10. Let targetByteOffset be target.[[ByteOffset]].
  11. Let srcName be the String value of source.[[TypedArrayName]].
  12. Let srcType be the Element Type value in Table 60 for srcName.
  13. Let srcElementSize be the Element Size value specified in Table 60 for srcName.
  14. Let srcLength be source.[[ArrayLength]].
  15. Let srcByteOffset be source.[[ByteOffset]].
  16. If targetOffset is +โˆž, throw a RangeError exception.
  17. If srcLength + targetOffset > targetLength, throw a RangeError exception.
  18. If target.[[ContentType]] โ‰  source.[[ContentType]], throw a TypeError exception.
  19. If both IsSharedArrayBuffer(srcBuffer) and IsSharedArrayBuffer(targetBuffer) are true, then
    1. If srcBuffer.[[ArrayBufferData]] and targetBuffer.[[ArrayBufferData]] are the same Shared Data Block values, let same be true; else let same be false.
  20. Else, let same be SameValue(srcBuffer, targetBuffer).
  21. If same is true, then
    1. Let srcByteLength be source.[[ByteLength]].
    2. Set srcBuffer to ? CloneArrayBuffer(srcBuffer, srcByteOffset, srcByteLength, %ArrayBuffer%).
    3. NOTE: %ArrayBuffer% is used to clone srcBuffer because is it known to not have any observable side-effects.
    4. Let srcByteIndex be 0.
  22. Else, let srcByteIndex be srcByteOffset.
  23. Let targetByteIndex be targetOffset ร— targetElementSize + targetByteOffset.
  24. Let limit be targetByteIndex + targetElementSize ร— srcLength.
  25. If srcType is the same as targetType, then
    1. NOTE: If srcType and targetType are the same, the transfer must be performed in a manner that preserves the bit-level encoding of the source data.
    2. Repeat, while targetByteIndex < limit,
      1. Let value be GetValueFromBuffer(srcBuffer, srcByteIndex, Uint8, true, Unordered).
      2. Perform SetValueInBuffer(targetBuffer, targetByteIndex, Uint8, value, true, Unordered).
      3. Set srcByteIndex to srcByteIndex + 1.
      4. Set targetByteIndex to targetByteIndex + 1.
  26. Else,
    1. Repeat, while targetByteIndex < limit,
      1. Let value be GetValueFromBuffer(srcBuffer, srcByteIndex, srcType, true, Unordered).
      2. Perform SetValueInBuffer(targetBuffer, targetByteIndex, targetType, value, true, Unordered).
      3. Set srcByteIndex to srcByteIndex + srcElementSize.
      4. Set targetByteIndex to targetByteIndex + targetElementSize.

23.2.3.23.2 SetTypedArrayFromArrayLike ( target, targetOffset, source )

The abstract operation SetTypedArrayFromArrayLike takes arguments target (a TypedArray object), targetOffset (a non-negative integer or +โˆž), and source (an ECMAScript value other than a TypedArray object). It sets multiple values in target, starting at index targetOffset, reading the values from source. It performs the following steps when called:

  1. Assert: source is any ECMAScript language value other than an Object with a [[TypedArrayName]] internal slot.
  2. Let targetBuffer be target.[[ViewedArrayBuffer]].
  3. If IsDetachedBuffer(targetBuffer) is true, throw a TypeError exception.
  4. Let targetLength be target.[[ArrayLength]].
  5. Let targetName be the String value of target.[[TypedArrayName]].
  6. Let targetElementSize be the Element Size value specified in Table 60 for targetName.
  7. Let targetType be the Element Type value in Table 60 for targetName.
  8. Let targetByteOffset be target.[[ByteOffset]].
  9. Let src be ? ToObject(source).
  10. Let srcLength be ? LengthOfArrayLike(src).
  11. If targetOffset is +โˆž, throw a RangeError exception.
  12. If srcLength + targetOffset > targetLength, throw a RangeError exception.
  13. Let targetByteIndex be targetOffset ร— targetElementSize + targetByteOffset.
  14. Let k be 0.
  15. Let limit be targetByteIndex + targetElementSize ร— srcLength.
  16. Repeat, while targetByteIndex < limit,
    1. Let Pk be ! ToString(๐”ฝ(k)).
    2. Let value be ? Get(src, Pk).
    3. If target.[[ContentType]] is BigInt, set value to ? ToBigInt(value).
    4. Otherwise, set value to ? ToNumber(value).
    5. If IsDetachedBuffer(targetBuffer) is true, throw a TypeError exception.
    6. Perform SetValueInBuffer(targetBuffer, targetByteIndex, targetType, value, true, Unordered).
    7. Set k to k + 1.
    8. Set targetByteIndex to targetByteIndex + targetElementSize.

23.2.3.24 %TypedArray%.prototype.slice ( start, end )

The interpretation and use of the arguments of %TypedArray%.prototype.slice are the same as for Array.prototype.slice as defined in 23.1.3.25. The following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. Let relativeStart be ? ToIntegerOrInfinity(start).
  5. If relativeStart is -โˆž, let k be 0.
  6. Else if relativeStart < 0, let k be max(len + relativeStart, 0).
  7. Else, let k be min(relativeStart, len).
  8. If end is undefined, let relativeEnd be len; else let relativeEnd be ? ToIntegerOrInfinity(end).
  9. If relativeEnd is -โˆž, let final be 0.
  10. Else if relativeEnd < 0, let final be max(len + relativeEnd, 0).
  11. Else, let final be min(relativeEnd, len).
  12. Let count be max(final - k, 0).
  13. Let A be ? TypedArraySpeciesCreate(O, ยซ ๐”ฝ(count) ยป).
  14. If count > 0, then
    1. If IsDetachedBuffer(O.[[ViewedArrayBuffer]]) is true, throw a TypeError exception.
    2. Let srcName be the String value of O.[[TypedArrayName]].
    3. Let srcType be the Element Type value in Table 60 for srcName.
    4. Let targetName be the String value of A.[[TypedArrayName]].
    5. Let targetType be the Element Type value in Table 60 for targetName.
    6. If srcType is different from targetType, then
      1. Let n be 0.
      2. Repeat, while k < final,
        1. Let Pk be ! ToString(๐”ฝ(k)).
        2. Let kValue be ! Get(O, Pk).
        3. Perform ! Set(A, ! ToString(๐”ฝ(n)), kValue, true).
        4. Set k to k + 1.
        5. Set n to n + 1.
    7. Else,
      1. Let srcBuffer be O.[[ViewedArrayBuffer]].
      2. Let targetBuffer be A.[[ViewedArrayBuffer]].
      3. Let elementSize be the Element Size value specified in Table 60 for Element Type srcType.
      4. NOTE: If srcType and targetType are the same, the transfer must be performed in a manner that preserves the bit-level encoding of the source data.
      5. Let srcByteOffset be O.[[ByteOffset]].
      6. Let targetByteIndex be A.[[ByteOffset]].
      7. Let srcByteIndex be (k ร— elementSize) + srcByteOffset.
      8. Let limit be targetByteIndex + count ร— elementSize.
      9. Repeat, while targetByteIndex < limit,
        1. Let value be GetValueFromBuffer(srcBuffer, srcByteIndex, Uint8, true, Unordered).
        2. Perform SetValueInBuffer(targetBuffer, targetByteIndex, Uint8, value, true, Unordered).
        3. Set srcByteIndex to srcByteIndex + 1.
        4. Set targetByteIndex to targetByteIndex + 1.
  15. Return A.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

23.2.3.25 %TypedArray%.prototype.some ( callbackfn [ , thisArg ] )

The interpretation and use of the arguments of %TypedArray%.prototype.some are the same as for Array.prototype.some as defined in 23.1.3.26.

When the some method is called with one or two arguments, the following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. If IsCallable(callbackfn) is false, throw a TypeError exception.
  5. Let k be 0.
  6. Repeat, while k < len,
    1. Let Pk be ! ToString(๐”ฝ(k)).
    2. Let kValue be ! Get(O, Pk).
    3. Let testResult be ! ToBoolean(? Call(callbackfn, thisArg, ยซ kValue, ๐”ฝ(k), O ยป)).
    4. If testResult is true, return true.
    5. Set k to k + 1.
  7. Return false.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

23.2.3.26 %TypedArray%.prototype.sort ( comparefn )

%TypedArray%.prototype.sort is a distinct function that, except as described below, implements the same requirements as those of Array.prototype.sort as defined in 23.1.3.27. The implementation of the %TypedArray%.prototype.sort specification may be optimized with the knowledge that the this value is an object that has a fixed length and whose integer-indexed properties are not sparse.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

Upon entry, the following steps are performed to initialize evaluation of the sort function. These steps are used instead of steps 1โ€“3 in 23.1.3.27:

  1. If comparefn is not undefined and IsCallable(comparefn) is false, throw a TypeError exception.
  2. Let obj be the this value.
  3. Let buffer be ? ValidateTypedArray(obj).
  4. Let len be obj.[[ArrayLength]].

The following version of SortCompare is used by %TypedArray%.prototype.sort. It performs a numeric comparison rather than the string comparison used in 23.1.3.27.

The abstract operation TypedArraySortCompare takes arguments x and y. It also has access to the comparefn and buffer values of the current invocation of the sort method. It performs the following steps when called:

  1. Assert: Both Type(x) and Type(y) are Number or both are BigInt.
  2. If comparefn is not undefined, then
    1. Let v be ? ToNumber(? Call(comparefn, undefined, ยซ x, y ยป)).
    2. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
    3. If v is NaN, return +0๐”ฝ.
    4. Return v.
  3. If x and y are both NaN, return +0๐”ฝ.
  4. If x is NaN, return 1๐”ฝ.
  5. If y is NaN, return -1๐”ฝ.
  6. If x < y, return -1๐”ฝ.
  7. If x > y, return 1๐”ฝ.
  8. If x is -0๐”ฝ and y is +0๐”ฝ, return -1๐”ฝ.
  9. If x is +0๐”ฝ and y is -0๐”ฝ, return 1๐”ฝ.
  10. Return +0๐”ฝ.
Note

Because NaN always compares greater than any other value, NaN property values always sort to the end of the result when comparefn is not provided.

23.2.3.27 %TypedArray%.prototype.subarray ( begin, end )

Returns a new TypedArray object whose element type is the same as this TypedArray and whose ArrayBuffer is the same as the ArrayBuffer of this TypedArray, referencing the elements at begin, inclusive, up to end, exclusive. If either begin or end is negative, it refers to an index from the end of the array, as opposed to from the beginning.

  1. Let O be the this value.
  2. Perform ? RequireInternalSlot(O, [[TypedArrayName]]).
  3. Assert: O has a [[ViewedArrayBuffer]] internal slot.
  4. Let buffer be O.[[ViewedArrayBuffer]].
  5. Let srcLength be O.[[ArrayLength]].
  6. Let relativeBegin be ? ToIntegerOrInfinity(begin).
  7. If relativeBegin is -โˆž, let beginIndex be 0.
  8. Else if relativeBegin < 0, let beginIndex be max(srcLength + relativeBegin, 0).
  9. Else, let beginIndex be min(relativeBegin, srcLength).
  10. If end is undefined, let relativeEnd be srcLength; else let relativeEnd be ? ToIntegerOrInfinity(end).
  11. If relativeEnd is -โˆž, let endIndex be 0.
  12. Else if relativeEnd < 0, let endIndex be max(srcLength + relativeEnd, 0).
  13. Else, let endIndex be min(relativeEnd, srcLength).
  14. Let newLength be max(endIndex - beginIndex, 0).
  15. Let constructorName be the String value of O.[[TypedArrayName]].
  16. Let elementSize be the Element Size value specified in Table 60 for constructorName.
  17. Let srcByteOffset be O.[[ByteOffset]].
  18. Let beginByteOffset be srcByteOffset + beginIndex ร— elementSize.
  19. Let argumentsList be ยซ buffer, ๐”ฝ(beginByteOffset), ๐”ฝ(newLength) ยป.
  20. Return ? TypedArraySpeciesCreate(O, argumentsList).

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

23.2.3.28 %TypedArray%.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )

%TypedArray%.prototype.toLocaleString is a distinct function that implements the same algorithm as Array.prototype.toLocaleString as defined in 23.1.3.29 except that the this value's [[ArrayLength]] internal slot is accessed in place of performing a [[Get]] of "length". The implementation of the algorithm may be optimized with the knowledge that the this value is an object that has a fixed length and whose integer-indexed properties are not sparse. However, such optimization must not introduce any observable changes in the specified behaviour of the algorithm.

This function is not generic. ValidateTypedArray is applied to the this value prior to evaluating the algorithm. If its result is an abrupt completion that exception is thrown instead of evaluating the algorithm.

Note

If the ECMAScript implementation includes the ECMA-402 Internationalization API this function is based upon the algorithm for Array.prototype.toLocaleString that is in the ECMA-402 specification.

23.2.3.29 %TypedArray%.prototype.toString ( )

The initial value of the %TypedArray%.prototype.toString data property is the same built-in function object as the Array.prototype.toString method defined in 23.1.3.30.

23.2.3.30 %TypedArray%.prototype.values ( )

The following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Return CreateArrayIterator(O, value).

23.2.3.31 %TypedArray%.prototype [ @@iterator ] ( )

The initial value of the @@iterator property is the same function object as the initial value of the %TypedArray%.prototype.values property.

23.2.3.32 get %TypedArray%.prototype [ @@toStringTag ]

%TypedArray%.prototype[@@toStringTag] is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Let O be the this value.
  2. If Type(O) is not Object, return undefined.
  3. If O does not have a [[TypedArrayName]] internal slot, return undefined.
  4. Let name be O.[[TypedArrayName]].
  5. Assert: Type(name) is String.
  6. Return name.

This property has the attributes { [[Enumerable]]: false, [[Configurable]]: true }.

The initial value of the "name" property of this function is "get [Symbol.toStringTag]".

23.2.4 Abstract Operations for TypedArray Objects

23.2.4.1 TypedArraySpeciesCreate ( exemplar, argumentList )

The abstract operation TypedArraySpeciesCreate takes arguments exemplar and argumentList. It is used to specify the creation of a new TypedArray object using a constructor function that is derived from exemplar. It performs the following steps when called:

  1. Assert: exemplar is an Object that has [[TypedArrayName]] and [[ContentType]] internal slots.
  2. Let defaultConstructor be the intrinsic object listed in column one of Table 60 for exemplar.[[TypedArrayName]].
  3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
  4. Let result be ? TypedArrayCreate(constructor, argumentList).
  5. Assert: result has [[TypedArrayName]] and [[ContentType]] internal slots.
  6. If result.[[ContentType]] โ‰  exemplar.[[ContentType]], throw a TypeError exception.
  7. Return result.

23.2.4.2 TypedArrayCreate ( constructor, argumentList )

The abstract operation TypedArrayCreate takes arguments constructor and argumentList. It is used to specify the creation of a new TypedArray object using a constructor function. It performs the following steps when called:

  1. Let newTypedArray be ? Construct(constructor, argumentList).
  2. Perform ? ValidateTypedArray(newTypedArray).
  3. If argumentList is a List of a single Number, then
    1. If newTypedArray.[[ArrayLength]] < โ„(argumentList[0]), throw a TypeError exception.
  4. Return newTypedArray.

23.2.4.3 ValidateTypedArray ( O )

The abstract operation ValidateTypedArray takes argument O. It performs the following steps when called:

  1. Perform ? RequireInternalSlot(O, [[TypedArrayName]]).
  2. Assert: O has a [[ViewedArrayBuffer]] internal slot.
  3. Let buffer be O.[[ViewedArrayBuffer]].
  4. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
  5. Return buffer.

23.2.5 The TypedArray Constructors

Each TypedArray constructor:

  • is an intrinsic object that has the structure described below, differing only in the name used as the constructor name instead of TypedArray, in Table 60.
  • is a function whose behaviour differs based upon the number and types of its arguments. The actual behaviour of a call of TypedArray depends upon the number and kind of arguments that are passed to it.
  • is not intended to be called as a function and will throw an exception when called in that manner.
  • is designed to be subclassable. It may be used as the value of an extends clause of a class definition. Subclass constructors that intend to inherit the specified TypedArray behaviour must include a super call to the TypedArray constructor to create and initialize the subclass instance with the internal state necessary to support the %TypedArray%.prototype built-in methods.
  • has a "length" property whose value is 3๐”ฝ.

23.2.5.1 TypedArray ( ...args )

Each TypedArray constructor performs the following steps when called:

  1. If NewTarget is undefined, throw a TypeError exception.
  2. Let constructorName be the String value of the Constructor Name value specified in Table 60 for this TypedArray constructor.
  3. Let proto be "%TypedArray.prototype%".
  4. Let numberOfArgs be the number of elements in args.
  5. If numberOfArgs = 0, then
    1. Return ? AllocateTypedArray(constructorName, NewTarget, proto, 0).
  6. Else,
    1. Let firstArgument be args[0].
    2. If Type(firstArgument) is Object, then
      1. Let O be ? AllocateTypedArray(constructorName, NewTarget, proto).
      2. If firstArgument has a [[TypedArrayName]] internal slot, then
        1. Perform ? InitializeTypedArrayFromTypedArray(O, firstArgument).
      3. Else if firstArgument has an [[ArrayBufferData]] internal slot, then
        1. If numberOfArgs > 1, let byteOffset be args[1]; else let byteOffset be undefined.
        2. If numberOfArgs > 2, let length be args[2]; else let length be undefined.
        3. Perform ? InitializeTypedArrayFromArrayBuffer(O, firstArgument, byteOffset, length).
      4. Else,
        1. Assert: Type(firstArgument) is Object and firstArgument does not have either a [[TypedArrayName]] or an [[ArrayBufferData]] internal slot.
        2. Let usingIterator be ? GetMethod(firstArgument, @@iterator).
        3. If usingIterator is not undefined, then
          1. Let values be ? IterableToList(firstArgument, usingIterator).
          2. Perform ? InitializeTypedArrayFromList(O, values).
        4. Else,
          1. NOTE: firstArgument is not an Iterable so assume it is already an array-like object.
          2. Perform ? InitializeTypedArrayFromArrayLike(O, firstArgument).
      5. Return O.
    3. Else,
      1. Assert: firstArgument is not an Object.
      2. Let elementLength be ? ToIndex(firstArgument).
      3. Return ? AllocateTypedArray(constructorName, NewTarget, proto, elementLength).

23.2.5.1.1 AllocateTypedArray ( constructorName, newTarget, defaultProto [ , length ] )

The abstract operation AllocateTypedArray takes arguments constructorName (a String which is the name of a TypedArray constructor in Table 60), newTarget, and defaultProto and optional argument length (a non-negative integer). It is used to validate and create an instance of a TypedArray constructor. If the length argument is passed, an ArrayBuffer of that length is also allocated and associated with the new TypedArray instance. AllocateTypedArray provides common semantics that is used by TypedArray. It performs the following steps when called:

  1. Let proto be ? GetPrototypeFromConstructor(newTarget, defaultProto).
  2. Let obj be ! IntegerIndexedObjectCreate(proto).
  3. Assert: obj.[[ViewedArrayBuffer]] is undefined.
  4. Set obj.[[TypedArrayName]] to constructorName.
  5. If constructorName is "BigInt64Array" or "BigUint64Array", set obj.[[ContentType]] to BigInt.
  6. Otherwise, set obj.[[ContentType]] to Number.
  7. If length is not present, then
    1. Set obj.[[ByteLength]] to 0.
    2. Set obj.[[ByteOffset]] to 0.
    3. Set obj.[[ArrayLength]] to 0.
  8. Else,
    1. Perform ? AllocateTypedArrayBuffer(obj, length).
  9. Return obj.

23.2.5.1.2 InitializeTypedArrayFromTypedArray ( O, srcArray )

The abstract operation InitializeTypedArrayFromTypedArray takes arguments O (a TypedArray object) and srcArray (a TypedArray object). It performs the following steps when called:

  1. Assert: O is an Object that has a [[TypedArrayName]] internal slot.
  2. Assert: srcArray is an Object that has a [[TypedArrayName]] internal slot.
  3. Let srcData be srcArray.[[ViewedArrayBuffer]].
  4. If IsDetachedBuffer(srcData) is true, throw a TypeError exception.
  5. Let constructorName be the String value of O.[[TypedArrayName]].
  6. Let elementType be the Element Type value in Table 60 for constructorName.
  7. Let elementLength be srcArray.[[ArrayLength]].
  8. Let srcName be the String value of srcArray.[[TypedArrayName]].
  9. Let srcType be the Element Type value in Table 60 for srcName.
  10. Let srcElementSize be the Element Size value specified in Table 60 for srcName.
  11. Let srcByteOffset be srcArray.[[ByteOffset]].
  12. Let elementSize be the Element Size value specified in Table 60 for constructorName.
  13. Let byteLength be elementSize ร— elementLength.
  14. If IsSharedArrayBuffer(srcData) is false, then
    1. Let bufferConstructor be ? SpeciesConstructor(srcData, %ArrayBuffer%).
  15. Else,
    1. Let bufferConstructor be %ArrayBuffer%.
  16. If elementType is the same as srcType, then
    1. Let data be ? CloneArrayBuffer(srcData, srcByteOffset, byteLength, bufferConstructor).
  17. Else,
    1. Let data be ? AllocateArrayBuffer(bufferConstructor, byteLength).
    2. If IsDetachedBuffer(srcData) is true, throw a TypeError exception.
    3. If srcArray.[[ContentType]] โ‰  O.[[ContentType]], throw a TypeError exception.
    4. Let srcByteIndex be srcByteOffset.
    5. Let targetByteIndex be 0.
    6. Let count be elementLength.
    7. Repeat, while count > 0,
      1. Let value be GetValueFromBuffer(srcData, srcByteIndex, srcType, true, Unordered).
      2. Perform SetValueInBuffer(data, targetByteIndex, elementType, value, true, Unordered).
      3. Set srcByteIndex to srcByteIndex + srcElementSize.
      4. Set targetByteIndex to targetByteIndex + elementSize.
      5. Set count to count - 1.
  18. Set O.[[ViewedArrayBuffer]] to data.
  19. Set O.[[ByteLength]] to byteLength.
  20. Set O.[[ByteOffset]] to 0.
  21. Set O.[[ArrayLength]] to elementLength.

23.2.5.1.3 InitializeTypedArrayFromArrayBuffer ( O, buffer, byteOffset, length )

The abstract operation InitializeTypedArrayFromArrayBuffer takes arguments O (a TypedArray object), buffer (an ArrayBuffer object), byteOffset (an ECMAScript language value), and length (an ECMAScript language value). It performs the following steps when called:

  1. Assert: O is an Object that has a [[TypedArrayName]] internal slot.
  2. Assert: buffer is an Object that has an [[ArrayBufferData]] internal slot.
  3. Let constructorName be the String value of O.[[TypedArrayName]].
  4. Let elementSize be the Element Size value specified in Table 60 for constructorName.
  5. Let offset be ? ToIndex(byteOffset).
  6. If offset modulo elementSize โ‰  0, throw a RangeError exception.
  7. If length is not undefined, then
    1. Let newLength be ? ToIndex(length).
  8. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
  9. Let bufferByteLength be buffer.[[ArrayBufferByteLength]].
  10. If length is undefined, then
    1. If bufferByteLength modulo elementSize โ‰  0, throw a RangeError exception.
    2. Let newByteLength be bufferByteLength - offset.
    3. If newByteLength < 0, throw a RangeError exception.
  11. Else,
    1. Let newByteLength be newLength ร— elementSize.
    2. If offset + newByteLength > bufferByteLength, throw a RangeError exception.
  12. Set O.[[ViewedArrayBuffer]] to buffer.
  13. Set O.[[ByteLength]] to newByteLength.
  14. Set O.[[ByteOffset]] to offset.
  15. Set O.[[ArrayLength]] to newByteLength / elementSize.

23.2.5.1.4 InitializeTypedArrayFromList ( O, values )

The abstract operation InitializeTypedArrayFromList takes arguments O (a TypedArray object) and values (a List of ECMAScript language values). It performs the following steps when called:

  1. Assert: O is an Object that has a [[TypedArrayName]] internal slot.
  2. Let len be the number of elements in values.
  3. Perform ? AllocateTypedArrayBuffer(O, len).
  4. Let k be 0.
  5. Repeat, while k < len,
    1. Let Pk be ! ToString(๐”ฝ(k)).
    2. Let kValue be the first element of values and remove that element from values.
    3. Perform ? Set(O, Pk, kValue, true).
    4. Set k to k + 1.
  6. Assert: values is now an empty List.

23.2.5.1.5 InitializeTypedArrayFromArrayLike ( O, arrayLike )

The abstract operation InitializeTypedArrayFromArrayLike takes arguments O (a TypedArray object) and arrayLike (an Object that is neither a TypedArray object nor an ArrayBuffer object). It performs the following steps when called:

  1. Assert: O is an Object that has a [[TypedArrayName]] internal slot.
  2. Let len be ? LengthOfArrayLike(arrayLike).
  3. Perform ? AllocateTypedArrayBuffer(O, len).
  4. Let k be 0.
  5. Repeat, while k < len,
    1. Let Pk be ! ToString(๐”ฝ(k)).
    2. Let kValue be ? Get(arrayLike, Pk).
    3. Perform ? Set(O, Pk, kValue, true).
    4. Set k to k + 1.

23.2.5.1.6 AllocateTypedArrayBuffer ( O, length )

The abstract operation AllocateTypedArrayBuffer takes arguments O (a TypedArray object) and length (a non-negative integer). It allocates and associates an ArrayBuffer with O. It performs the following steps when called:

  1. Assert: O is an Object that has a [[ViewedArrayBuffer]] internal slot.
  2. Assert: O.[[ViewedArrayBuffer]] is undefined.
  3. Let constructorName be the String value of O.[[TypedArrayName]].
  4. Let elementSize be the Element Size value specified in Table 60 for constructorName.
  5. Let byteLength be elementSize ร— length.
  6. Let data be ? AllocateArrayBuffer(%ArrayBuffer%, byteLength).
  7. Set O.[[ViewedArrayBuffer]] to data.
  8. Set O.[[ByteLength]] to byteLength.
  9. Set O.[[ByteOffset]] to 0.
  10. Set O.[[ArrayLength]] to length.
  11. Return O.

23.2.6 Properties of the TypedArray Constructors

Each TypedArray constructor:

  • has a [[Prototype]] internal slot whose value is %TypedArray%.
  • has a "name" property whose value is the String value of the constructor name specified for it in Table 60.
  • has the following properties:

23.2.6.1 TypedArray.BYTES_PER_ELEMENT

The value of TypedArray.BYTES_PER_ELEMENT is the Element Size value specified in Table 60 for TypedArray.

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

23.2.6.2 TypedArray.prototype

The initial value of TypedArray.prototype is the corresponding TypedArray prototype intrinsic object (23.2.7).

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

23.2.7 Properties of the TypedArray Prototype Objects

Each TypedArray prototype object:

  • has a [[Prototype]] internal slot whose value is %TypedArray.prototype%.
  • is an ordinary object.
  • does not have a [[ViewedArrayBuffer]] or any other of the internal slots that are specific to TypedArray instance objects.

23.2.7.1 TypedArray.prototype.BYTES_PER_ELEMENT

The value of TypedArray.prototype.BYTES_PER_ELEMENT is the Element Size value specified in Table 60 for TypedArray.

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

23.2.7.2 TypedArray.prototype.constructor

The initial value of a TypedArray.prototype.constructor is the corresponding %TypedArray% intrinsic object.

23.2.8 Properties of TypedArray Instances

TypedArray instances are Integer-Indexed exotic objects. Each TypedArray instance inherits properties from the corresponding TypedArray prototype object. Each TypedArray instance has the following internal slots: [[TypedArrayName]], [[ViewedArrayBuffer]], [[ByteLength]], [[ByteOffset]], and [[ArrayLength]].