DEV Community

Cover image for What are Typed Arrays in JavaScript?
Hossein Naseri
Hossein Naseri

Posted on

What are Typed Arrays in JavaScript?

An array in JavaScript is like a big, messy room or warehouse where you can store anything: tables, random-sized boxes, bags, etc.
Or technically: strings, numbers, objects, or even other arrays.

This flexibility is awesome, but it comes with a cost:
the array can become slow and inefficient when it gets large.🐌

Imagine trying to find one specific box in a chaotic room full of different-sized items. You have to search through everything.
Now imagine you have a separate, well-organized room where every box is the same size and contains only one type of item.
You can go straight to the exact spot and grab what you need super fast!🏎️

This is the main idea behind Typed Arrays.

Typed arrays are special arrays where:

  • Every element must be a number of a fixed size (8-bit, 16-bit, 32-bit, etc.).
  • The size and type of each element is decided when you create the array.
  • They are designed for high performance and working with binary data (raw bytes).

Why do we use Typed Arrays?

Computers love perfectly organized data! Typed Arrays give them exactly that with:

  • Super Speed: Because the computer knows exactly what kind of data is inside and how much space each item takes, it can read, write, and process the data much faster. This is very important for games, video, audio, graphics, and large data processing.⚑
  • Memory Efficiency: Regular JavaScript arrays waste memory. Typed Arrays use the smallest possible amount of memory by storing raw binary data directly.πŸ“¦
  • Direct access to binary data: They are perfect for working with files, images, audio, 3D models, WebGL, WebSockets, and more.🌐

Common Types of Typed Arrays:

Here are the most commonly used Typed Arrays:

Typed Array Stores Size per element Common Use Cases
Uint8Array Whole numbers 0 to 255 8 bits (1 byte) Images, colors, binary files
Int8Array Numbers from -128 to 127 8 bits Small signed values
Uint16Array Whole numbers 0 to 65,535 16 bits Audio samples, larger values
Int16Array Numbers from -32,768 to 32,767 16 bits Audio, sensor data
Uint32Array Whole numbers 0 to 4.29 billion 32 bits Large counters, graphics
Int32Array Numbers from -2.1B to 2.1B 32 bits General math, coordinates
Float32Array Decimal numbers (e.g., 3.14159) 32 bits 3D positions, physics, graphics
Float64Array High-precision decimal numbers 64 bits Scientific calculations

Uint8Array is the most commonly used, especially when starting.

Quick ExampleπŸ“

// Regular array (flexible but slower for big data)
const normal = [1, "hello", true, 3.14];

// Typed Array - only numbers 0-255
const pixels = new Uint8Array([255, 128, 0, 255]); 

console.log(pixels[0]);     // 255
pixels[1] = 200;            // works
// pixels[1] = "hello";     // Won't work! Converts to 0 because it only allows numbers.
Enter fullscreen mode Exit fullscreen mode

Key Points to Remember:

  • Typed Arrays have a fixed size once created.🎯
  • They can only store numbers of one specific type.
  • They are significantly faster and more memory-efficient than regular arrays when working with large amounts of data.
  • In Node.js, Buffer is commonly used and is built on top of Uint8Array. -πŸ’‘ Bonus Tip (Under the Hood): Typed Arrays don't actually hold the data themselves. They are just a "view" looking inside a raw chunk of memory called an ArrayBuffer.

Top comments (0)