Skip to content

Commit 6a6c00c

Browse files
committed
Initial type-checking verifier, see #526, added to bench out of competition
1 parent a5ad0c1 commit 6a6c00c

12 files changed

+384
-171
lines changed

README.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ greeter.sayHello({ name: 'you' }, function(err, response) {
191191
});
192192
```
193193

194-
To make this work, all you have to do is provide an `rpcImpl`, which is is an asynchronous function that takes the reflected service method, the binary HelloRequest and a node-style callback as its parameters. For example:
194+
To make this work, all you have to do is provide an `rpcImpl`, which is an asynchronous function that takes the reflected service method, the binary HelloRequest and a node-style callback as its parameters. For example:
195195

196196
```js
197197
function rpcImpl(method, requestData, callback) {
@@ -219,7 +219,7 @@ The library exports a flat `protobuf` namespace with the following members, orde
219219

220220
### Parser
221221

222-
* **load(filename: `string|Array`, [root: `Root`], [callback: `function(err: Error, [root: Root])`]): `Promise|undefined`** [[source](https://github.com/dcodeIO/protobuf.js/blob/master/src/index.js)]<br />
222+
* **load(filename: `string|string[]`, [root: `Root`], [callback: `function(err: Error, [root: Root])`]): `Promise|undefined`** [[source](https://github.com/dcodeIO/protobuf.js/blob/master/src/index.js)]<br />
223223
Loads one or multiple .proto or preprocessed .json files into a common root namespace.
224224

225225
* **loadSync(filename: `string|string[]`, [root: `Root`]): `Root`** [[source](https://github.com/dcodeIO/protobuf.js/blob/master/src/index.js)]<br />
@@ -234,10 +234,10 @@ The library exports a flat `protobuf` namespace with the following members, orde
234234
* **package: `string|undefined`**<br />
235235
The package name, if declared.
236236

237-
* **imports: `Array|undefined`**<br />
237+
* **imports: `string[]|undefined`**<br />
238238
File names of imported files, if any.
239239

240-
* **weakImports: `Array|undefined`**<br />
240+
* **weakImports: `string[]|undefined`**<br />
241241
File names of weakly imported files, if any.
242242

243243
* **syntax: `string|undefined`**<br />
@@ -413,33 +413,33 @@ The package includes a benchmark that tries to compare performance to native JSO
413413
```
414414
benchmarking encoding performance ...
415415
416-
Type.encode to buffer x 471,717 ops/sec ±1.30% (91 runs sampled)
417-
JSON.stringify to string x 310,406 ops/sec ±1.00% (90 runs sampled)
418-
JSON.stringify to buffer x 172,766 ops/sec ±1.20% (84 runs sampled)
416+
Type.encode to buffer x 479,876 ops/sec ±0.64% (92 runs sampled)
417+
JSON.stringify to string x 311,489 ops/sec ±0.84% (87 runs sampled)
418+
JSON.stringify to buffer x 175,079 ops/sec ±1.48% (82 runs sampled)
419419
420420
Type.encode to buffer was fastest
421-
JSON.stringify to string was 34.0% slower
422-
JSON.stringify to buffer was 63.3% slower
421+
JSON.stringify to string was 35.2% slower
422+
JSON.stringify to buffer was 63.8% slower
423423
424424
benchmarking decoding performance ...
425425
426-
Type.decode from buffer x 1,285,867 ops/sec ±0.70% (90 runs sampled)
427-
JSON.parse from string x 292,106 ops/sec ±1.00% (89 runs sampled)
428-
JSON.parse from buffer x 259,361 ops/sec ±0.92% (90 runs sampled)
426+
Type.decode from buffer x 1,267,612 ops/sec ±1.18% (90 runs sampled)
427+
JSON.parse from string x 291,707 ops/sec ±1.12% (92 runs sampled)
428+
JSON.parse from buffer x 262,640 ops/sec ±0.77% (89 runs sampled)
429429
430430
Type.decode from buffer was fastest
431-
JSON.parse from string was 77.4% slower
432-
JSON.parse from buffer was 79.9% slower
431+
JSON.parse from string was 77.0% slower
432+
JSON.parse from buffer was 79.2% slower
433433
434434
benchmarking combined performance ...
435435
436-
Type to/from buffer x 238,382 ops/sec ±0.96% (89 runs sampled)
437-
JSON to/from string x 127,352 ops/sec ±0.73% (93 runs sampled)
438-
JSON to/from buffer x 89,593 ops/sec ±0.85% (87 runs sampled)
436+
Type to/from buffer x 248,897 ops/sec ±0.89% (90 runs sampled)
437+
JSON to/from string x 126,848 ops/sec ±0.75% (92 runs sampled)
438+
JSON to/from buffer x 89,854 ops/sec ±0.79% (93 runs sampled)
439439
440440
Type to/from buffer was fastest
441-
JSON to/from string was 46.5% slower
442-
JSON to/from buffer was 62.4% slower
441+
JSON to/from string was 49.0% slower
442+
JSON to/from buffer was 63.9% slower
443443
```
444444

445445
Note that JSON is a native binding nowadays and as such is about as fast as it possibly can get. So, how can protobuf.js be faster?

bench/index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ for (var i = 0; i < 500000; ++i)
3131
Test.encode(data).finish();
3232
for (var i = 0; i < 1000000; ++i)
3333
Test.decode(buf);
34+
for (var i = 0; i < 500000; ++i)
35+
Test.verify(data);
3436
console.log("");
3537

3638
// give the optimizer some time to do its job
@@ -52,7 +54,7 @@ setTimeout(function() {
5254

5355
newSuite("decoding")
5456
.add("Type.decode from buffer", function() {
55-
Test.decode(buf);
57+
Test.decode(buf); // no allocation overhead, if you wondered
5658
})
5759
.add("JSON.parse from string", function() {
5860
JSON.parse(str);
@@ -74,4 +76,12 @@ setTimeout(function() {
7476
})
7577
.run();
7678

79+
newSuite("verifying")
80+
.add("Type.verify", function() {
81+
var r = Test.verify(data);
82+
if (r)
83+
throw Error(r);
84+
})
85+
.run();
86+
7787
}, 3000);

0 commit comments

Comments
 (0)