-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
index.js
90 lines (77 loc) · 2.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"use strict";
// NOTE: This benchmark partly compares apples and oranges in that it measures protocol buffers,
// which is purely a binary format, and JSON, which is purely a string format.
//
// This matters because strings aren't actually transfered over the network but must still be
// converted to binary somewhere down the road. Because this can't be measured reliably, this
// benchmark compares both pure string performance of JSON and additional binary conversion of the
// same data using node buffers. Actual JSON performance on the network level should be somewhere
// in between.
var newSuite = require("./suite"),
payload = require("./data/bench.json");
var Buffer_from = Buffer.from !== Uint8Array.from && Buffer.from || function(value, encoding) { return new Buffer(value, encoding); };
// protobuf.js dynamic: load the proto and set up a buffer
var pbjsCls = require("..").loadSync(require.resolve("./data/bench.proto")).resolveAll().lookup("Test");
var pbjsMsg = payload; // alt: pbjsCls.fromObject(payload);
var pbjsBuf = pbjsCls.encode(pbjsMsg).finish();
// protobuf.js static: load the proto
var pbjsStaticCls = require("./data/static_pbjs.js").Test;
// JSON: set up a string and a buffer
var jsonMsg = payload;
var jsonStr = JSON.stringify(jsonMsg);
var jsonBuf = Buffer_from(jsonStr, "utf8");
// google-protobuf: load the proto, set up an Uint8Array and a message
var jspbCls = require("./data/static_jspb.js").Test;
var jspbBuf = new Uint8Array(Array.prototype.slice.call(pbjsBuf));
var jspbMsg = jspbCls.deserializeBinary(jspbBuf);
newSuite("encoding")
.add("protobuf.js (reflect)", function() {
pbjsCls.encode(pbjsMsg).finish();
})
.add("protobuf.js (static)", function() {
pbjsStaticCls.encode(pbjsMsg).finish();
})
.add("JSON (string)", function() {
JSON.stringify(jsonMsg);
})
.add("JSON (buffer)", function() {
Buffer_from(JSON.stringify(jsonMsg), "utf8");
})
.add("google-protobuf", function() {
jspbMsg.serializeBinary();
})
.run();
newSuite("decoding")
.add("protobuf.js (reflect)", function() {
pbjsCls.decode(pbjsBuf); // no allocation overhead, if you wondered
})
.add("protobuf.js (static)", function() {
pbjsStaticCls.decode(pbjsBuf);
})
.add("JSON (string)", function() {
JSON.parse(jsonStr);
})
.add("JSON (buffer)", function() {
JSON.parse(jsonBuf.toString("utf8"));
})
.add("google-protobuf", function() {
jspbCls.deserializeBinary(jspbBuf);
})
.run();
newSuite("combined")
.add("protobuf.js (reflect)", function() {
pbjsCls.decode(pbjsCls.encode(pbjsMsg).finish());
})
.add("protobuf.js (static)", function() {
pbjsStaticCls.decode(pbjsStaticCls.encode(pbjsMsg).finish());
})
.add("JSON (string)", function() {
JSON.parse(JSON.stringify(jsonMsg));
})
.add("JSON (buffer)", function() {
JSON.parse(Buffer_from(JSON.stringify(jsonMsg), "utf8").toString("utf8"));
})
.add("google-protobuf", function() {
jspbCls.deserializeBinary(jspbMsg.serializeBinary());
})
.run();