dataview vs typed array views

JavaScript performance comparison

Revision 6 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var source = new Uint8Array([0x32, 0x65, 0x42, 0x56]);
  var nativeEndian = new Uint16Array(new Uint8Array([0x12, 0x34]).buffer)[0] === 0x3412;
  
  var buf = new ArrayBuffer(1600);
  
  function peek(bytes, littleEndian) {
      var i, _i, _j;
      if (littleEndian == null) {
        littleEndian = false;
      }
      if (littleEndian === nativeEndian) {
        for (i = _i = 0; _i < bytes; i = _i += 1) {
          uint8[i] = source[i];
        }
      } else {
        for (i = _j = 0; _j < bytes; i = _j += 1) {
          uint8[bytes - i - 1] = source[i];
        }
      }
    }
  
  
  
  var buf2 = new ArrayBuffer(10);
  var uint8_2 = new Uint8Array(buf2);
  
  
  function peek2(bytes) {
    for (var i = 0; i < bytes; i++) {
      uint8_2[i] = source[i];
    }
  }
  
  function readUInt32_1(position, littleEndian) {
   // peek(4, littleEndian);
    return uint32[position/4];
  }
  function readUInt32_2(position, littleEndian) {
   // peek2(4);
    return view.getUint32(position, littleEndian);
  }
  
  function readUInt32_3(position, littleEndian) {
    var a0 = source[position],
        a1 = source[position + 1],
        a2 = source[position + 2],
        a3 = source[position + 3];
    
    if (littleEndian)
      return ((a3 << 24) >>> 0) + (a2 << 16) + (a1 << 8) + (a0);
    else
      return ((a0 << 24) >>> 0) + (a1 << 16) + (a2 << 8) + (a3);
  }

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Typed Arrays
for(var i=0; i<1600; i+=4) {
/*
var uint8 = new Uint8Array(buf);
var int8 = new Int8Array(buf);
var uint16 = new Uint16Array(buf);
var int16 = new Int16Array(buf);
var int32 = new Int32Array(buf);
var float32 = new Float32Array(buf);
var float64 = new Float64Array(buf);
*/
var uint32 = new Uint32Array(buf);

readUInt32_1(i);
}
pending…
DataView
for(var i=0; i<1600; i += 4) {
var view = new DataView(buf);
readUInt32_2(i);
}
pending…
manual
for(var i=0; i<1600; i += 4) {
var uint8 = new Uint8Array(buf);
readUInt32_3(i);
}
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.

0 Comments