-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathmsgpack-benchmark.js
146 lines (133 loc) · 4.11 KB
/
msgpack-benchmark.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* eslint-disable no-console */
// based on https://github.com/endel/msgpack-benchmark
"use strict";
require("ts-node/register");
const Benchmark = require("benchmark");
const msgpackEncode = require("..").encode;
const msgpackDecode = require("..").decode;
const ExtensionCodec = require("..").ExtensionCodec;
const float32ArrayExtensionCodec = new ExtensionCodec();
float32ArrayExtensionCodec.register({
type: 0x01,
encode: (object) => {
if (object instanceof Float32Array) {
return new Uint8Array(object.buffer, object.byteOffset, object.byteLength);
}
return null;
},
decode: (data) => {
const copy = new Uint8Array(data.byteLength);
copy.set(data);
return new Float32Array(copy.buffer);
},
});
const float32ArrayZeroCopyExtensionCodec = new ExtensionCodec();
float32ArrayZeroCopyExtensionCodec.register({
type: 0x01,
encode: (object) => {
if (object instanceof Float32Array) {
return (pos) => {
const bpe = Float32Array.BYTES_PER_ELEMENT;
const padding = 1 + ((bpe - ((pos + 1) % bpe)) % bpe);
const data = new Uint8Array(object.buffer);
const result = new Uint8Array(padding + data.length);
result[0] = padding;
result.set(data, padding);
return result;
};
}
return null;
},
decode: (data) => {
const padding = data[0];
const bpe = Float32Array.BYTES_PER_ELEMENT;
const offset = data.byteOffset + padding;
const length = data.byteLength - padding;
return new Float32Array(data.buffer, offset, length / bpe);
},
});
const implementations = {
"@msgpack/msgpack": {
encode: msgpackEncode,
decode: msgpackDecode,
},
"@msgpack/msgpack (Float32Array extension)": {
encode: (data) => msgpackEncode(data, { extensionCodec: float32ArrayExtensionCodec }),
decode: (data) => msgpackDecode(data, { extensionCodec: float32ArrayExtensionCodec }),
},
"@msgpack/msgpack (Float32Array with zero-copy extension)": {
encode: (data) => msgpackEncode(data, { extensionCodec: float32ArrayZeroCopyExtensionCodec }),
decode: (data) => msgpackDecode(data, { extensionCodec: float32ArrayZeroCopyExtensionCodec }),
},
"msgpack-lite": {
encode: require("msgpack-lite").encode,
decode: require("msgpack-lite").decode,
},
"notepack.io": {
encode: require("notepack.io/browser/encode"),
decode: require("notepack.io/browser/decode"),
},
};
const samples = [
{
// exactly the same as:
// https://raw.githubusercontent.com/endel/msgpack-benchmark/master/sample-large.json
name: "./sample-large.json",
data: require("./sample-large.json"),
},
{
name: "Large array of numbers",
data: [
{
position: new Array(1e3).fill(1.14),
},
],
},
{
name: "Large Float32Array",
data: [
{
position: new Float32Array(1e3).fill(1.14),
},
],
},
];
function validate(name, data, encoded) {
return JSON.stringify(data) === JSON.stringify(implementations[name].decode(encoded));
}
for (const sample of samples) {
const { name: sampleName, data } = sample;
const encodeSuite = new Benchmark.Suite();
const decodeSuite = new Benchmark.Suite();
console.log("");
console.log("**" + sampleName + ":** (" + JSON.stringify(data).length + " bytes in JSON)");
console.log("");
for (const name of Object.keys(implementations)) {
implementations[name].toDecode = implementations[name].encode(data);
if (!validate(name, data, implementations[name].toDecode)) {
console.log("```");
console.log("Not supported by " + name);
console.log("```");
continue;
}
encodeSuite.add("(encode) " + name, () => {
implementations[name].encode(data);
});
decodeSuite.add("(decode) " + name, () => {
implementations[name].decode(implementations[name].toDecode);
});
}
encodeSuite.on("cycle", (event) => {
console.log(String(event.target));
});
console.log("```");
encodeSuite.run();
console.log("```");
console.log("");
decodeSuite.on("cycle", (event) => {
console.log(String(event.target));
});
console.log("```");
decodeSuite.run();
console.log("```");
}