Skip to content

Typed Arrays

Find out what Typed Arrays are and how to use them

JavaScript Provides 8 Typed Array types:

all of them are ArrayBufferView instances.

A Typed Array is essentially a view into an ArrayBuffer, where every item has the same size, and type.

DataView is another view into an ArrayBuffer, but in this case the items in the array can have different sizes and types.

Here’s an example of how to create an array of 8-bit signed integers:

const a = new Int8Array()

You can pre-allocate n bytes:

const bytes = 1024
const a = new Int8Array(bytes)

The main use is to allow to look into an ArrayBuffer, which on its own is opaque (we can’t inspect its content).

Here’s how we do so:

//we got this `buffer` ArrayBuffer
const a = new Int8Array(buffer)

Those typed arrays are array-like, so now we can inspect the content of the buffer via the usual array access techniques, and we have access to lots of methods and properties including map(), reduce() and so on.

The main use case for Typed Arrays is to use with WebGL, Web Audio or the Canvas API. Some of the WebGL functions are expecting typed arrays, as they are much more performant than regular JavaScript arrays.

One thing to keep in mind is that typed arrays don’t let us control the endianness: it uses the byte order of the platform. In general this works out fine, because the main use case as we said is to use the array locally, using one of the multimedia APIs. Also, most consumer computers use little endian since Intel uses that convention. But, if you transfer the data of a Typed Array on a system that uses big endian, the data might be badly encoded and, as such, invalid.

In case you need this kind of control over endianness, use DataView instead.


download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about platform: