a robot on a desk

JavaScript TypedArray

Introduction to JavaScript TypedArray

JavaScript TypedArray is a built-in object that provides a way to store and manipulate binary data in a fixed-length, typed format. It allows efficient handling of data such as integers, floating-point numbers, and raw binary data.

TypedArray objects are similar to arrays, but they have a fixed length and a specific data type. They are particularly useful when working with large amounts of data or when performing low-level operations that require direct memory access.

Types of TypedArray

JavaScript provides several types of TypedArray objects, each corresponding to a different data type:

  • Int8Array: Represents a one-byte signed integer.
  • Uint8Array: Represents a one-byte unsigned integer.
  • Int16Array: Represents a two-byte signed integer.
  • Uint16Array: Represents a two-byte unsigned integer.
  • Int32Array: Represents a four-byte signed integer.
  • Uint32Array: Represents a four-byte unsigned integer.
  • Float32Array: Represents a four-byte floating-point number.
  • Float64Array: Represents an eight-byte floating-point number.

Creating a TypedArray

To create a TypedArray, you can use the constructor of the respective TypedArray type:


// Create an Int32Array with 5 elements
const intArray = new Int32Array(5);

// Create a Float64Array with an existing array buffer
const buffer = new ArrayBuffer(16);
const floatArray = new Float64Array(buffer);

Accessing and Modifying TypedArray Elements

TypedArray elements can be accessed and modified using the index notation, just like regular arrays:


const intArray = new Int32Array([1, 2, 3, 4, 5]);

console.log(intArray[0]); // Output: 1

intArray[1] = 10;
console.log(intArray); // Output: Int32Array [1, 10, 3, 4, 5]

Working with TypedArray Methods

TypedArray objects provide various methods for manipulating and transforming the data:

  • forEach: Executes a provided function once for each element in the TypedArray.
  • map: Creates a new TypedArray with the results of calling a provided function on every element in the TypedArray.
  • reduce: Applies a function against an accumulator and each element in the TypedArray to reduce it to a single value.
  • slice: Returns a new TypedArray containing a copy of a portion of the original TypedArray.
  • sort: Sorts the elements of the TypedArray in place.

Example: Calculating the Sum of Integers

Let’s see an example that demonstrates how to use a TypedArray to calculate the sum of integers:


const intArray = new Int32Array([1, 2, 3, 4, 5]);

const sum = intArray.reduce((accumulator, currentValue) => accumulator + currentValue);

console.log(sum); // Output: 15

Example: Converting TypedArray to Regular Array

Sometimes, you may need to convert a TypedArray to a regular array. You can achieve this using the spread operator or the Array.from() method:


const intArray = new Int32Array([1, 2, 3, 4, 5]);

const regularArray1 = [...intArray];
console.log(regularArray1); // Output: [1, 2, 3, 4, 5]

const regularArray2 = Array.from(intArray);
console.log(regularArray2); // Output: [1, 2, 3, 4, 5]

Conclusion

JavaScript TypedArray provides a powerful way to work with binary data in a typed format. It allows for efficient manipulation of data and enables low-level operations in JavaScript. By understanding the different types of TypedArray and their methods, you can leverage this feature to perform complex data processing tasks.

Scroll to Top