JSON vs binary parsing

JavaScript performance comparison

Revision 7 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var json = '{ "function": "moveCharacterTo", "parameters": [1234, -4567] }';
  var x = 1234;
  var y = -4567;
  var id = 5000;
  var buffer = new ArrayBuffer(6);
  var view = new Uint16Array(buffer, 0, 1);
  view[0] = id;
  view = new Int16Array(view.buffer, 2, 2);
  view[0] = x;
  view[1] = y;
  
  
  buffer = view.buffer;
  nonbuffer = view
  
  
  var buffer = new ArrayBuffer(1000);
  var int8view = new Uint8Array(buffer);
  for(var i=0; i<1000; i++) {
    int8view[i] = i % 255;
  }
  
  var int8buffer = int8view.buffer;
  var int8array = new Uint8Array(int8buffer);
  var dataview = new DataView(int8buffer);

};
</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
JSON parsing
var obj = JSON.parse(json);
var function_name = obj.function;
var parameters = obj.parameters;
pending…
binary parse
var view = new Uint16Array(buffer);
var id_unpacked = view[0];
view = new Int16Array(buffer, 2, 2);
var x_unpacked = view[0];
var y_unpacked = view[1];
pending…
Array using an array
var view = new Uint16Array(nonbuffer);
var id_unpacked = view[0];
view = new Int16Array(nonbuffer, 2, 2);
var x_unpacked = view[0];
var y_unpacked = view[1];
pending…
var view = new Uint8Array(int8buffer);

var x = 0
for(var i=0; i<1000; i++) {
  x += int8buffer[i]
}
pending…
var view = new Uint8Array(int8view);

var x = 0
for(var i=0; i<1000; i++) {
  x += int8buffer[i]
}
pending…
var view = new Uint8Array(int8buffer);

var x = 0
for(var i=0; i<1000; i++) {
  x += view[i]
}
pending…
var x = 0
for(var i=0; i<1000; i++) {
  x += int8array[i]
}
pending…
var view = new Uint8Array(int8view);

var x = 0
for(var i=0; i<1000; i++) {
  x += view[i]
}
pending…

var x = 0
for(var i=0; i<1000; i++) {
  x += dataview.getUint8(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