Skip to content

Commit

Permalink
Removed as-function overload for Reader/Writer, profiler stub, optimi…
Browse files Browse the repository at this point in the history
…zed version of Reader#int32
  • Loading branch information
dcodeIO committed Dec 6, 2016
1 parent 53a16bf commit a46cc49
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 180 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,33 +368,33 @@ The package includes a [benchmark](https://github.com/dcodeIO/protobuf.js/tree/m
```
benchmarking encoding performance ...
Type.encode to buffer x 469,818 ops/sec ±0.58% (89 runs sampled)
JSON.stringify to string x 309,883 ops/sec ±0.81% (92 runs sampled)
JSON.stringify to buffer x 174,440 ops/sec ±1.46% (87 runs sampled)
Type.encode to buffer x 471,717 ops/sec ±1.30% (91 runs sampled)
JSON.stringify to string x 310,406 ops/sec ±1.00% (90 runs sampled)
JSON.stringify to buffer x 172,766 ops/sec ±1.20% (84 runs sampled)
Type.encode to buffer was fastest
JSON.stringify to string was 34.2% slower
JSON.stringify to buffer was 63.2% slower
JSON.stringify to string was 34.0% slower
JSON.stringify to buffer was 63.3% slower
benchmarking decoding performance ...
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 x 1,285,867 ops/sec ±0.70% (90 runs sampled)
JSON.parse from string x 292,106 ops/sec ±1.00% (89 runs sampled)
JSON.parse from buffer x 259,361 ops/sec ±0.92% (90 runs sampled)
Type.decode from buffer was fastest
JSON.parse from string was 73.8% slower
JSON.parse from buffer was 76.4% slower
JSON.parse from string was 77.4% slower
JSON.parse from buffer was 79.9% slower
benchmarking combined performance ...
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 x 238,382 ops/sec ±0.96% (89 runs sampled)
JSON to/from string x 127,352 ops/sec ±0.73% (93 runs sampled)
JSON to/from buffer x 89,593 ops/sec ±0.85% (87 runs sampled)
Type to/from buffer was fastest
JSON to/from string was 46.0% slower
JSON to/from buffer was 60.7% slower
JSON to/from string was 46.5% slower
JSON to/from buffer was 62.4% slower
```

Note that JSON is a native binding nowadays and as such is *really* fast. So, how can protobuf.js be faster?
Expand Down
29 changes: 29 additions & 0 deletions bench/prof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var fs = require("fs"),
path = require("path");
var protobuf = require("..");

// A profiling stub to measure just raw encoding / decoding performance using benchmark data.

var root = protobuf.parse(fs.readFileSync(require.resolve("../bench/bench.proto")).toString("utf8")).root;
var Test = root.lookup("Test");
var data = data = require("../bench/bench.json");

var count = process.argv.length > 3 && parseInt(process.argv[3]) || 10000000;

var r = protobuf.Reader.create(new Buffer(0));

switch (process.argv[2]) {
default:
console.log("usage: " + path.basename(process.argv[1]) + " <encode|decode> [iterations=10000000]");
process.exit(1);
return;
case "encode":
for (var i = 0; i < count; ++i)
Test.encode(data).finish();
break;
case "decode":
var buf = Test.encode(data).finish();
for (var i = 0; i < count; ++i)
Test.decode(buf);
break;
}
68 changes: 39 additions & 29 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

84 changes: 0 additions & 84 deletions scripts/prof.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var Enum = require("./enum"),
decoder.fallback = function fallback(reader, length) {
/* eslint-disable no-invalid-this, block-scoped-var, no-redeclare */
var fields = this.getFieldsById(),
reader = reader instanceof Reader ? reader : Reader(reader),
reader = reader instanceof Reader ? reader : Reader.create(reader),
limit = length === undefined ? reader.len : reader.pos + length,
message = new (this.getCtor())();
while (reader.pos < limit) {
Expand Down Expand Up @@ -92,7 +92,7 @@ decoder.generate = function generate(mtype) {
var fields = mtype.getFieldsArray();
var gen = util.codegen("r", "l")

("r instanceof Reader||(r=Reader(r))")
("r instanceof Reader||(r=Reader.create(r))")
("var c=l===undefined?r.len:r.pos+l,m=new(this.getCtor())")
("while(r.pos<c){")
("var t=r.tag()")
Expand Down
4 changes: 2 additions & 2 deletions src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var Enum = require("./enum"),
encoder.fallback = function fallback(message, writer) {
/* eslint-disable block-scoped-var, no-redeclare */
if (!writer)
writer = Writer();
writer = Writer.create();
var fields = this.getFieldsArray(), fi = 0;
while (fi < fields.length) {
var field = fields[fi++].resolve(),
Expand Down Expand Up @@ -99,7 +99,7 @@ encoder.generate = function generate(mtype) {
/* eslint-disable no-unexpected-multiline */
var fields = mtype.getFieldsArray();
var gen = util.codegen("m", "w")
("w||(w=Writer())");
("w||(w=Writer.create())");

for (var i = 0; i < fields.length; ++i) {
var field = fields[i].resolve(),
Expand Down
Loading

0 comments on commit a46cc49

Please sign in to comment.