Day 26: Typed Arrays

dhrn

Dharan Ganesan

Posted on August 15, 2023

Day 26: Typed Arrays

Typed Arrays were introduced as part of the ECMAScript 2015 (ES6) specification to address the limitations of traditional arrays when dealing with binary data. Unlike traditional arrays, where elements can be of mixed data types, Typed Arrays enforce a single data type for all elements within the array. This alignment enables better memory utilisation and more predictable behaviour.

  1. Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array: These arrays deal with signed and unsigned 8, 16, and 32-bit integers.
  2. Float32Array, Float64Array: These arrays work with 32-bit and 64-bit floating-point numbers.
  3. BigInt64Array, BigUint64Array: Introduced in later specifications, these arrays handle 64-bit signed and unsigned integers.

Problem

Imagine you're building a real-time audio processing application. You need to process raw audio data, where each audio sample is a 16-bit signed integer.

The Older Way

In the past, developers had to rely on standard arrays to handle binary data.

const audioSamples = [0.1, 0.5, -0.3, ...]; // Traditional JavaScript array

// Manipulate audio samples
const processedSamples = audioSamples.map(sample => sample * 2);
Enter fullscreen mode Exit fullscreen mode

However, the dynamic nature of arrays introduces memory overhead, as each element requires additional space to store the type information and other metadata.

Typed Array

const audioSamples = new Float32Array([0.1, 0.5, -0.3, ...]); // Typed Array

// Manipulate audio samples efficiently
const processedSamples = audioSamples.map(sample => sample * 2);
Enter fullscreen mode Exit fullscreen mode

By utilising Typed Arrays, you directly operate on the raw binary data, avoiding unnecessary overhead and benefiting from improved performance.

Advantages

  1. 🚀 Performance: Typed Arrays offer better performance compared to traditional arrays for tasks involving binary data manipulation.
  2. 🧐 Memory Efficiency: With a fixed data type, Typed Arrays are more memory-efficient as they eliminate the need for type information and other metadata per element.
  3. 🔧 Predictable Behavior: Since data types are enforced, you can be confident in the expected behavior and avoid type-related bugs.
  4. 🔗 Interoperability: Typed Arrays can be used with other APIs like WebGL, Web Audio, and Fetch API for seamless integration.

Limitations

  1. ⚠️ Fixed Size: Once created, the size of a Typed Array is fixed and cannot be changed dynamically.
  2. 🚫 Limited Data Types: While there are several supported data types, the variety is not as extensive as with traditional arrays.

So, next time you're working on a project that involves crunching numbers or dealing with raw binary data, give Typed Arrays a try and witness the difference in speed and efficiency!

💖 💪 🙅 🚩
dhrn
Dharan Ganesan

Posted on August 15, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Day 26: Typed Arrays
javascript Day 26: Typed Arrays

August 15, 2023