Skip to content

Commit

Permalink
Also optimize reader with what we have learned
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 5, 2016
1 parent d83f799 commit f91c432
Show file tree
Hide file tree
Showing 13 changed files with 342 additions and 50 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ protobuf.js [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [
[paypal-image]: https://img.shields.io/badge/paypal-donate-yellow.svg
[paypal-url]: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=dcode%40dcode.io&item_name=%3C3%20protobuf.js

**Recommended read:** [Changes in protobuf.js 6.0](https://github.com/dcodeIO/protobuf.js/wiki/Changes-in-protobuf.js-6.0)

Features
--------
* Lightning fast through [runtime code generation](#performance)
* Optimized [for performance](#performance)
* Exhaustive [browser support](#compatibility)
* Managed [TypeScript definitions](#usage-with-typescript)
* Elaborate [API documentation](#documentation)
Expand Down Expand Up @@ -374,23 +376,23 @@ JSON.stringify to buffer x 174,440 ops/sec ±1.46% (87 runs sampled)
benchmarking decoding performance ...
Type.decode from buffer x 976,639 ops/sec ±0.81% (91 runs sampled)
JSON.parse from string x 294,282 ops/sec ±0.69% (89 runs sampled)
JSON.parse from buffer x 263,440 ops/sec ±0.84% (93 runs sampled)
Type.decode from buffer x 1,127,271 ops/sec ±0.76% (90 runs sampled)
JSON.parse from string x 295,445 ops/sec ±0.74% (92 runs sampled)
JSON.parse from buffer x 265,703 ops/sec ±0.85% (92 runs sampled)
Type.decode from buffer was fastest
JSON.parse from string was 69.8% slower
JSON.parse from buffer was 73.0% slower
JSON.parse from string was 73.8% slower
JSON.parse from buffer was 76.4% slower
benchmarking combined performance ...
Type to/from buffer x 205,029 ops/sec ±1.59% (88 runs sampled)
JSON to/from string x 127,198 ops/sec ±0.76% (91 runs sampled)
JSON to/from buffer x 90,980 ops/sec ±0.77% (91 runs sampled)
Type to/from buffer x 232,255 ops/sec ±0.99% (87 runs sampled)
JSON to/from string x 125,555 ops/sec ±1.01% (91 runs sampled)
JSON to/from buffer x 91,243 ops/sec ±0.83% (91 runs sampled)
Type to/from buffer was fastest
JSON to/from string was 37.5% slower
JSON to/from buffer was 55.3% slower
JSON to/from string was 46.0% slower
JSON to/from buffer was 60.7% slower
```

Note that JSON is a native binding nowadays and as such is *really* fast. So, how can protobuf.js be faster?
Expand Down
2 changes: 1 addition & 1 deletion bench/bench.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"outer" : {
"bool" : [ true, false, false, true, false, false, true ],
"double": 0.5
"double": 204.8
}
},
"float": 0.25
Expand Down
6 changes: 4 additions & 2 deletions bench/bench.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ message Test {
string string = 1;
uint32 uint32 = 2;
Inner inner = 3;
float float = 4;
float float = 4; // make sure to set something that's fair to JSON

message Inner {

Expand Down Expand Up @@ -34,5 +34,7 @@ message Test {
message Outer {

repeated bool bool = 1;
double double = 2;
double double = 2; // make sure to set something that's fair to JSON
}

// bytes cannot be used
133 changes: 133 additions & 0 deletions bench/read.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
var protobuf = require("../src/index"),
newSuite = require("./suite"),
ieee754 = require("../lib/ieee754");

// This benchmark compares raw data type performance of Uint8Array and Buffer.

var data = [ 0xCD, 0xCC, 0xCC, 0x3D ]; // ~0.10000000149011612 LE

var array = new Uint8Array(data);
var buffer = Buffer.from(data);

var f64 = new Float64Array(1);
var f32 = new Float32Array(f64.buffer);
var f8b = new Uint8Array(f64.buffer);

// raw float read speed
newSuite("float")
.add("ieee754 array", function() {
ieee754.read(array, 0, false, 23, 4);
})
.add("ieee754 buffer", function() {
ieee754.read(buffer, 0, false, 23, 4);
})
.add("f32 array", function() {
var pos = 0;
f8b[pos++] = array[0];
f8b[pos++] = array[1];
f8b[pos++] = array[2];
f8b[pos ] = array[3];
return f32[0];
})
.add("f32 buffer", function() {
var pos = 0;
f8b[pos++] = buffer[0];
f8b[pos++] = buffer[1];
f8b[pos++] = buffer[2];
f8b[pos ] = buffer[3];
return f32[0];
})
.add("readFloatLE buffer", function() {
buffer.readFloatLE(0, true);
})
.run();

var data = [ 0x9A, 0x99, 0x99, 0x99, 0x99, 0x99, 0xB9, 0x3F ]; // 0.1 LE

var array = new Uint8Array(data);
var buffer = Buffer.from(data);

// raw double read speed
newSuite("double")
.add("ieee754 array", function() {
ieee754.read(array, 0, false, 52, 8);
})
.add("ieee754 buffer", function() {
ieee754.read(buffer, 0, false, 52, 8);
})
.add("f64 array", function() {
var pos = 0;
f8b[pos++] = array[0];
f8b[pos++] = array[1];
f8b[pos++] = array[2];
f8b[pos++] = array[3];
f8b[pos++] = array[4];
f8b[pos++] = array[5];
f8b[pos++] = array[6];
f8b[pos ] = array[7];
return f64[0];
})
.add("f64 buffer", function() {
var pos = 0;
f8b[pos++] = buffer[0];
f8b[pos++] = buffer[1];
f8b[pos++] = buffer[2];
f8b[pos++] = buffer[3];
f8b[pos++] = buffer[4];
f8b[pos++] = buffer[5];
f8b[pos++] = buffer[6];
f8b[pos ] = buffer[7];
return f64[0];
})
.add("readDoubleLE buffer", function() {
buffer.readDoubleLE(0, true);
})
.run();

function readString(bytes) {
var len = bytes.length;
if (len) {
var out = new Array(len), p = 0, c = 0;
while (p < len) {
var c1 = bytes[p++];
if (c1 < 128)
out[c++] = c1;
else if (c1 > 191 && c1 < 224)
out[c++] = (c1 & 31) << 6 | bytes[p++] & 63;
else if (c1 > 239 && c1 < 365) {
var u = ((c1 & 7) << 18 | (bytes[p++] & 63) << 12 | (bytes[p++] & 63) << 6 | bytes[p++] & 63) - 0x10000;
out[c++] = 0xD800 + (u >> 10);
out[c++] = 0xDC00 + (u & 1023);
} else
out[c++] = (c1 & 15) << 12 | (bytes[p++] & 63) << 6 | bytes[p++] & 63;
}
return String.fromCharCode.apply(String, out.slice(0, c));
}
}

// raw string read speed
[
"Lorem ipsu",
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore ipsum."
].forEach(function(str) {

var buffer = Buffer.from(str, "utf8"),
array = new Uint8Array(buffer.length);
for (var i = 0; i < buffer.length; ++i)
array[i] = buffer[i];

newSuite("string[" + str.length + "]")
.add("readString array", function() {
readString(array);
})
.add("readString buffer", function() {
readString(buffer)
})
.add("toString buffer", function() {
buffer.toString("utf8", 0, buffer.length);
})
.add("utf8Slice buffer", function() {
buffer.utf8Slice(0, buffer.length);
})
.run();
});
3 changes: 1 addition & 2 deletions bench/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var protobuf = require("../src/index"),

// This benchmark compares raw data type performance of Uint8Array and Buffer.

/* var array = new Uint8Array(8);
var array = new Uint8Array(8);
var buffer = new Buffer(8);

// raw fixed32 write speed
Expand Down Expand Up @@ -119,7 +119,6 @@ newSuite("bytes")
source.copy(buffer, 0);
})
.run();
*/

function writeString(buf, pos, val) {
for (var i = 0; i < val.length; ++i) {
Expand Down
Loading

0 comments on commit f91c432

Please sign in to comment.