Typed Arrays (JavaScript)

You can use typed arrays to handle binary data from sources such as network protocols, binary file formats, and raw graphics buffers. Typed arrays can also be used to manage in-memory binary data with well-known byte layouts.

The following code shows how to use an ArrayBuffer Object as the response of an XMLHttpRequest. You can manipulate the bytes in the response by using the different methods of the DataView Object, or by copying the bytes into the appropriate typed array.

Tip Tip

For more information about using different response types with an XmlHttpRequest, see XMLHttpRequest.responseType, XMLHttpRequest enhancements, and Downloading different types of content (Windows Store apps).

...
<div id="xhrDiv"></div>
...
var name = "http://www.microsoft.com";
var xhrDiv = document.getElementById("xhrDiv");

var req = new XMLHttpRequest();
req.open("GET", name, true);
req.responseType = "arraybuffer";
req.onreadystatechange = function () {
if (req.readyState == req.DONE) {
    var arrayResponse = req.response;
    var dataView = new DataView(arrayResponse);
    var ints = new Uint32Array(dataView.byteLength / 4);

    xhrDiv.style.backgroundColor = "#00FF00";
    xhrDiv.innerText = "Array is " + ints.length + "uints long";
    }
}
req.send();

An ArrayBuffer Object represents a buffer of raw data that is used to store data for the different typed arrays. You cannot read from or write to an ArrayBuffer, but you can pass it to a typed array or DataView Object to interpret the raw buffer. You can use an ArrayBuffer to store any kind of data (or mixed types of data).

You can use a DataView Object to read and write the different kinds of binary data to any location in the ArrayBuffer.

The typed array types represent views of an ArrayBuffer Object that can be indexed and manipulated. All array types are of fixed length.

Name

Size (in bytes)

Description

Int8Array Object

1

Eight-bit two's complement signed integer

Uint8Array Object

1

Eight-bit unsigned integer

Int16Array Object

2

Sixteen-bit two's complement signed integer

Uint16Array Object

2

Sixteen-bit unsigned integer

Int32Array Object

4

Thirty-two-bit two's complement signed integer

Uint32Array Object

4

Thirty-two-bit unsigned integer

Float32Array Object

4

Thirty-two-bit IEEE floating point

Float64Array Object

8

Sixty-four-bit IEEE floating point

Show:
© 2014 Microsoft