Uint32Array vs DataView (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script>
  function CustomView(buffer) {
    this.buffer = buffer;
    this.u8 = new Uint8Array(buffer);
    this.u32 = new Uint32Array(buffer);
  }

  CustomView.prototype.getUint32 = function (index) {
    return this.u32[(index/4)|0];
  }

  CustomView.prototype.setUint32 = function (index, value) {
    this.u32[(index/4)|0] = value;
  }


  CustomView.prototype.getUint32_2 = function (i) {
    return (this.u8[i+3] << 24) | (this.u8[i+2] << 16) | (this.u8[i+1] << 8) | this.u8[i];
  }

  CustomView.prototype.setUint32_2 = function (index, value) {
    this.u8[index] = (value) & 0xff;
    this.u8[index+1] = (value >> 8) & 0xff;
    this.u8[index+2] = (value >> 16) & 0xff;
    this.u8[index+3] = (value >> 24) &0xff;
  }

  var len = 1024;
  var len2 = len/4;
  var a = new ArrayBuffer(len);
  var u32 = new Uint32Array(a);
  var dv = new DataView(a);
  var cv = new CustomView(a);
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
DataView write
for (var ii = 0; ii < len; ii+=4) {
  dv.setUint32(ii, ii);
}
ready
Uint8 write
for (var ii = 0; ii < len2; ++ii) {
  u32[ii] = ii;
}
ready
DataView read
var sum=0;
for (var ii = 0; ii < len; ii+=4) {
  sum += dv.getUint32(ii);
}
ready
Uint8 read
var sum=0;
for (var ii = 0; ii < len2; ++ii) {
  sum += u32[ii];
}
ready
CustomView write
for (var ii = 0; ii < len; ii+=4) {
  cv.setUint32(ii, ii);
}
ready
CustomView read
var sum=0;
for (var ii = 0; ii < len; ii+=4) {
  sum += cv.getUint32(ii);
}
ready
CustomView write2
for (var ii = 0; ii < len; ii+=4) {
  cv.setUint32_2(ii, ii);
}
ready
CustomView read2
var sum=0;
for (var ii = 0; ii < len; ii+=4) {
  sum += cv.getUint32_2(ii);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.