TypedArray demonstrates an array-like view of a binary data buffer. They are introduced in ECMAScript version 6 to handle binary data. There is no keyword reserved 'TypedArray', nor is there a directly visible TypedArray constructor.
There are several types of TypedArray shown below with their range, size, web IDL Type, and Equivalent C Type:
Type
| Range Value
| Size (Bytes)
| Web IDL Type
| Equivalent C type
|
---|
Int8Array | -128 to 127 | 1 | byte | int8_t |
Uint8Array | 0 to 255 | 1 | octet | uint8_t |
Uint8ClampedArray | 0 to 255 | 1 | octet | uint8_t |
Int16Array | -32768 to 32767 | 2 | short | int16_t |
Uint16Array | 0 to 65535 | 2 | unsigned short | uint16_t |
Int32Array | -2147483648 to 2147483647 | 4 | long | int32_t |
Uint32Array | 0 to 4294967295 | 4 | unsigned long | uint32_t |
Float32Array | 1.2×10^-38 to 3.4×10^38 | 4 | unrestricted float | float |
Float64Array | 5.0×10^-324 to 1.8×10^308 | 8 | unrestricted double | double |
BigInt64Array | -2^63 to 2^63-1 | 8 | bigint | int64_t (signed long) |
BigUint64Array | 0 to 2^64-1 | 8 | bigint | uint64_t (unsigned long) |
Constructor: This object cannot be instantiated directly. Instead, you have to create an instance of an array of a particular type, such as an UInt8Array or a BigInt64Array.
When creating an instance of a TypedArray(e.g. Int8Array), an Array Buffer instance is also created internally in the memory or, if an ArrayBuffer object is given as a constructor argument, then that argument is used instead.
We can create the instance of TypedArray by using the 'new' keyword with the respective constructor.
Syntax:
new TypedArray();
// Or
new TypedArray(length);
// Or
new TypedArray(typedArray);
// Or
new TypedArray(object);
// Or
new TypedArray(buffer [, byteOffset [, length]]);
TypedArray can be of any type mentioned above.
Parameters: Below are the parameters that TypedArray can take:
- length: The size of ArrayBuffer to create.
- typedArray: Type of array to be created.
- object: When called with the object argument, a new typed array instance is created.
- buffer, byteOffset, length: When called with a buffer, byte offset, and a length argument, a new typed array view is created that is used to view the specified ArrayBuffer.
Instantiating a TypedArray: Below are some examples that show how to instantiate a TypedArray.
Example 1: In this example, we will initialize a typedArray.
JavaScript
const buffer = new ArrayBuffer(8);
// Initiating using the constructor
const uint8 = new Uint8Array(buffer);
// Output is 8 as we initiated the length with 8
console.log(uint8.length);
Output:
8
Example 2: In this example, we will initialize a typedArray.
JavaScript
const int8 = new Int8Array([0, 0, 0, 0]);
int8.fill(4, 1, 3);
console.log(int8);
Output:
Int8Array(4) [0, 4, 4, 0,
buffer: ArrayBuffer(4),
byteLength: 4, byteOffset: 0,
length: 4,
Symbol(Symbol.toStringTag): 'Int8Array']
Example 3: Checking if the values are contained inside the TypedArray.
JavaScript
const int8 = new Int8Array([10, 20, 30, 40, 50]);
console.log(int8.includes(20));
console.log(int8.includes(20, 3));
Output:
true
false
Similar Reads
JavaScript typedArray.@@iterator The typedArray.@@iterator is an inbuilt property in JavaScript which is used to return the initial value of the given typedArray's element. Syntax: arr[Symbol.iterator]() Parameters: It does not accept any parameter because it is a property not a function. Return value: It returns the array iterator
1 min read
JavaScript typedArray.set() Method The typedArray.set() is an inbuilt function in JavaScript which is used to stores a number of values in the given typedArray. typedArray.set(typedArray, offset) Parameters: It accept two parameters which are specified below- typedarray: It is the source array. offset: It is optional and it is into t
1 min read
JavaScript typedArray.entries() with Examples The Javascript typedArray.entries() is an inbuilt function in JavaScript that gives a new array iterator object containing the key and value pairs of the given typedArray object. Syntax: typedArray.entries() Parameter: It does not accept any parameters. Return value It returns a new array iterator o
2 min read
Subarray meaning in DSA A subarray is a portion of an array that consists of consecutive elements from the original array. SubarrayCharacteristics of a Subarray:Contiguity: The elements in a subarray are contiguous, meaning they are consecutive and in order in the original array.Length: The length of a subarray can be any
2 min read
JavaScript typedArray.subarray() with Examples The Javascript typedArray.subarray() is an inbuilt function in JavaScript that is used to return a part of the typedArray object. Syntax:typedarray.subarray(begin, end);Parameters: begin: It specifies the index of the starting element from which the part of the given array is to be started. It is op
2 min read
Applications, Advantages and Disadvantages of Array Array is a linear data structure that is a collection of data elements of same types. Arrays are stored in contiguous memory locations. It is a static data structure with a fixed size.Table of ContentApplications of Array Data Structure:Advantages of Array Data Structure:Disadvantages of Array Data
2 min read
Solidity - Reference Types Solidity references store and modify complicated data structures including arrays, structs, maps, and strings. These data types hold a reference to the data's memory or storage location, unlike value types like integers and booleans. Reference types are vital for developing complicated Ethereum smar
6 min read
C# | Implicitly Typed Arrays Implicitly typed arrays are those arrays in which the type of the array is deduced from the element specified in the array initializer. The implicitly typed arrays are similar to implicitly typed variable. In general, implicitly typed arrays are used in the query expression.Important points about im
3 min read
Array Definition & Meaning in DSA An array is a collection of items of same data type stored at contiguous memory locations Array exampleTypes of Arrays:One-dimensional Array: It is the simplest kind of array where all the elements are stored linearly in a single row. Two-dimensional Array: A two-dimensional array is an array with 2
4 min read
Initialize an ArrayList in Java ArrayList is a part of the collection framework and is present in java.util package. It provides us dynamic arrays in Java. Though it may be slower than standard arrays, but can be helpful in programs where lots of manipulation in the array is needed.ArrayList inherits the AbstractList class and imp
4 min read