JS Typed Arrays
JS Typed Arrays
for accessing raw binary data. As you may already know, Array objects
grow and shrink dynamically and can have any JavaScript value. JavaScript
engines perform optimizations so that these arrays are fast. However, as
web applications become more and more powerful, adding features such
as audio and video manipulation, access to raw data using WebSockets,
and so forth, it has become clear that there are times when it would be
helpful for JavaScript code to be able to quickly and easily manipulate raw
binary data in typed arrays.
However, typed arrays are not to be confused with normal arrays, as
calling Array.isArray() on a typed array returns false. Moreover, not
all methods available for normal arrays are supported by typed arrays
(e.g. push and pop).
Buffers and views: typed array architecture
To achieve maximum flexibility and efficiency, JavaScript typed arrays split
the implementation into buffers and views. A buffer (implemented by
the ArrayBufferobject) is an object representing a chunk of data; it has
no format to speak of, and offers no mechanism for accessing its
contents. In order to access the memory contained in a buffer, you need
to use a view. A view provides a context that is, a data type, starting
offset, and number of elements that turns the data into an actual
typed array.
ArrayBuffer
The ArrayBuffer is a data type that is used to represent a generic, fixed-
length binary data buffer. You can't directly manipulate the contents of
an ArrayBuffer; instead, you create a typed array view or
a DataView which represents the buffer in a specific format, and use that
to read and write the contents of the buffer.
DataView
The DataView is a low-level interface that provides a getter/setter API to
read and write arbitrary data to the buffer. This is useful when dealing
with different types of data, for example. Typed array views are in the
native byte-order (see Endianness) of your platform. With a DataView you
are able to control the byte-order. It is big-endian by default and can be
set to little-endian in the getter/setter methods.
Web APIs using typed arrays
FileReader.prototype.readAsArrayBuffer()
The FileReader.prototype.readAsArrayBuffer() method starts
reading the contents of the specified Blob or File.
XMLHttpRequest.prototype.send()
XMLHttpRequest instances' send() method now supports typed
arrays and ArrayBuffer objects as argument.
ImageData.data
Is a Uint8ClampedArray representing a one-dimensional array
containing the data in the RGBA order, with integer values
between 0 and 255 inclusive.
Examples
Using views with buffers
First of all, we will need to create a buffer, here with a fixed length of 16-
bytes:
int16View[0] = 32;
console.log('Entry 0 in the 32-bit array is now ' +
int32View[0]);
The output from this is "Entry 0 in the 32-bit array is now 32". In other
words, the two arrays are indeed simply views on the same data buffer,
treating it as different formats. You can do this with any view types.
struct someStruct {
unsigned long id;
char username[16];
float amountDue;
};
You can access a buffer containing data in this format like this:
Specifications
Specification Status Comment
Typed Array Specification Obsolete Superseded by ECMAScript 2015.
ECMAScript 2015 (6th Edition, ECMA-
262) Initial definition in an ECMA
Standard
The definition of 'TypedArray Objects' in that standard.
specification.
Browser compatibility
Escritorio
Mvil
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 7.0 4.0 (2) 10 11.6 5.1