DataView vs. TypedArrayViews

JavaScript performance comparison

Test case created by Niklas Werner

Preparation code

<script>
// ----------------------------------------------------------------
// from https://developer.mozilla.org/en-US/docs/Web/API/DataView
var littleEndian = (function() {
  var buffer = new ArrayBuffer(2);
  new DataView(buffer).setInt16(0, 256, true);
  return new Int16Array(buffer)[0] === 256;
})();
// ----------------------------------------------------------------

var byteBuffer = new ArrayBuffer(1);
var byteArray = new Uint8Array(byteBuffer);
var byteView = new DataView(byteBuffer);

var floatBuffer = new ArrayBuffer(4);
var floatArray = new Float32Array(floatBuffer);
var floatView = new DataView(floatBuffer);
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
TypedArrayView - byte
byteArray[0] = 1;
var b = byteArray[0];
pending…
DataView - byte
byteView.setUint8(0, 1);
var b = byteView.getUint8(0);
pending…
TypedArrayView - float
floatArray[0] = 1;
var f = floatArray[0];
pending…
DataView - float
floatView.setFloat32(0, 1, littleEndian);
var f = floatView.getFloat32(0, littleEndian);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment