Permalink
Browse files

Also optimize reader with what we have learned

  • Loading branch information...
dcodeIO committed Dec 5, 2016
1 parent d83f799 commit f91c432a498bebc0adecef1562061b392611f51a
Showing with 342 additions and 50 deletions.
  1. +13 −11 README.md
  2. +1 −1 bench/bench.json
  3. +4 −2 bench/bench.proto
  4. +133 −0 bench/read.js
  5. +1 −2 bench/write.js
  6. +92 −15 dist/protobuf.js
  7. +1 −1 dist/protobuf.js.map
  8. +3 −3 dist/protobuf.min.js
  9. BIN dist/protobuf.min.js.gz
  10. +1 −1 dist/protobuf.min.js.map
  11. +2 −0 package.json
  12. +83 −6 src/reader.js
  13. +8 −8 src/writer.js
@@ -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)
@@ -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?
@@ -14,7 +14,7 @@
},
"outer" : {
"bool" : [ true, false, false, true, false, false, true ],
"double": 0.5
"double": 204.8
}
},
"float": 0.25
@@ -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 {
@@ -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
@@ -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();
});
@@ -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
@@ -119,7 +119,6 @@ newSuite("bytes")
source.copy(buffer, 0);
})
.run();
*/
function writeString(buf, pos, val) {
for (var i = 0; i < val.length; ++i) {
Oops, something went wrong.

0 comments on commit f91c432

Please sign in to comment.