cbor-x 0.9.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -21
- package/decode.d.ts +5 -0
- package/decode.js +344 -118
- package/dist/index.js +650 -327
- package/dist/index.min.js +100 -107
- package/dist/node-index.js +93 -0
- package/dist/node.cjs +651 -325
- package/dist/test.js +107 -9
- package/encode.d.ts +1 -1
- package/encode.js +302 -144
- package/index.d.ts +1 -1
- package/index.js +2 -4
- package/node-index.js +2 -4
- package/package.json +13 -4
- package/.vscode/launch.json +0 -22
- package/tests/benchmark-stream.cjs +0 -282
- package/tests/benchmark.cjs +0 -245
- package/tests/example.json +0 -52
- package/tests/example2.json +0 -26
- package/tests/example3.json +0 -22
- package/tests/example4.json +0 -1
- package/tests/example5.json +0 -12
- package/tests/floats.json +0 -1
- package/tests/index.html +0 -28
- package/tests/sample-large.json +0 -231
- package/tests/strings2.json +0 -1
- package/tests/test-compatibility.cjs +0 -36
- package/tests/test-incomplete.js +0 -41
- package/tests/test-node-iterators.js +0 -72
- package/tests/test-node-stream.js +0 -60
- package/tests/test.js +0 -586
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { Decoder, decode, addExtension, FLOAT32_OPTIONS, clearSource, roundFloat32 } from './decode.js'
|
|
1
|
+
export { Decoder, decode, addExtension, FLOAT32_OPTIONS, clearSource, roundFloat32, isNativeAccelerationEnabled } from './decode.js'
|
|
2
2
|
export { Encoder, encode } from './encode.js'
|
|
3
3
|
import { Transform, Readable } from 'stream'
|
|
4
4
|
|
package/index.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
export { Encoder, addExtension, encode, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } from './encode.js'
|
|
2
|
-
export { Tag, Decoder, decodeMultiple, decode, FLOAT32_OPTIONS, clearSource, roundFloat32 } from './decode.js'
|
|
1
|
+
export { Encoder, addExtension, encode, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT, REUSE_BUFFER_MODE } from './encode.js'
|
|
2
|
+
export { Tag, Decoder, decodeMultiple, decode, FLOAT32_OPTIONS, clearSource, roundFloat32, isNativeAccelerationEnabled } from './decode.js'
|
|
3
3
|
export { decodeIter, encodeIter } from './iterators.js'
|
|
4
|
-
export const useRecords = false
|
|
5
|
-
export const mapsAsObjects = true
|
package/node-index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { Encoder, addExtension, encode, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT, REUSE_BUFFER_MODE } from './encode.js'
|
|
2
|
-
export { Tag, Decoder, decodeMultiple, decode, FLOAT32_OPTIONS, clearSource, roundFloat32 } from './decode.js'
|
|
2
|
+
export { Tag, Decoder, decodeMultiple, decode, FLOAT32_OPTIONS, clearSource, roundFloat32, isNativeAccelerationEnabled } from './decode.js'
|
|
3
3
|
export { EncoderStream, DecoderStream } from './stream.js'
|
|
4
4
|
export { decodeIter, encodeIter } from './iterators.js'
|
|
5
5
|
export const useRecords = false
|
|
@@ -16,9 +16,7 @@ function tryRequire(moduleId) {
|
|
|
16
16
|
let require = createRequire(import.meta.url)
|
|
17
17
|
return require(moduleId)
|
|
18
18
|
} catch (error) {
|
|
19
|
-
if (typeof window
|
|
20
|
-
console.warn('Native extraction module not loaded, cbor-x will still run, but with decreased performance. ' + error.message.split('\n')[0])
|
|
21
|
-
else
|
|
19
|
+
if (typeof window != 'undefined')
|
|
22
20
|
console.warn('For browser usage, directly use cbor-x/decode or cbor-x/encode modules. ' + error.message.split('\n')[0])
|
|
23
21
|
}
|
|
24
22
|
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cbor-x",
|
|
3
3
|
"author": "Kris Zyp",
|
|
4
|
-
"version": "
|
|
5
|
-
"description": "Ultra-fast CBOR implementation with tag extensions
|
|
4
|
+
"version": "1.1.0",
|
|
5
|
+
"description": "Ultra-fast and conformant CBOR (RFC 8949) implementation with support for numerous tag extensions including records and structured cloning",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"types": "./index.d.ts",
|
|
8
8
|
"main": "./dist/node.cjs",
|
|
@@ -52,10 +52,17 @@
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
},
|
|
55
|
+
"files": [
|
|
56
|
+
"/dist",
|
|
57
|
+
"*.md",
|
|
58
|
+
"/*.js",
|
|
59
|
+
"/*.ts"
|
|
60
|
+
],
|
|
55
61
|
"optionalDependencies": {
|
|
56
|
-
"cbor-extract": "^0.
|
|
62
|
+
"cbor-extract": "^1.0.0"
|
|
57
63
|
},
|
|
58
64
|
"devDependencies": {
|
|
65
|
+
"@rollup/plugin-json": "^4.1.0",
|
|
59
66
|
"@types/node": "latest",
|
|
60
67
|
"async": "^3",
|
|
61
68
|
"cbor": "^5",
|
|
@@ -64,7 +71,9 @@
|
|
|
64
71
|
"esm": "^3.2.25",
|
|
65
72
|
"mocha": "^8.1.3",
|
|
66
73
|
"rollup": "^1.20.3",
|
|
67
|
-
"@rollup/plugin-json": "^4.1.0",
|
|
68
74
|
"rollup-plugin-babel-minify": "^9.0.0"
|
|
75
|
+
},
|
|
76
|
+
"dependencies": {
|
|
77
|
+
"esbuild": "^0.13.15"
|
|
69
78
|
}
|
|
70
79
|
}
|
package/.vscode/launch.json
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
// Use IntelliSense to learn about possible attributes.
|
|
3
|
-
// Hover to view descriptions of existing attributes.
|
|
4
|
-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
-
"version": "0.2.0",
|
|
6
|
-
"configurations": [
|
|
7
|
-
{
|
|
8
|
-
"type": "pwa-node",
|
|
9
|
-
"request": "launch",
|
|
10
|
-
"name": "Run cbor-x tests",
|
|
11
|
-
"runtimeArgs": [
|
|
12
|
-
"test"
|
|
13
|
-
],
|
|
14
|
-
"runtimeExecutable": "npm",
|
|
15
|
-
"skipFiles": [
|
|
16
|
-
"<node_internals>/**"
|
|
17
|
-
],
|
|
18
|
-
"console": "internalConsole",
|
|
19
|
-
"outputCapture": "std"
|
|
20
|
-
}
|
|
21
|
-
]
|
|
22
|
-
}
|
|
@@ -1,282 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
var PassThrough = require("stream").PassThrough;
|
|
4
|
-
var async = require("async");
|
|
5
|
-
|
|
6
|
-
let { EncoderStream, DecoderStream } = require("..");
|
|
7
|
-
var msgpack = require("msgpack-lite");
|
|
8
|
-
var Encoder = require("msgpack-lite/lib/encoder").Encoder;
|
|
9
|
-
var Decoder = require("msgpack-lite/lib/decoder").Decoder;
|
|
10
|
-
var notepack = require("notepack");
|
|
11
|
-
|
|
12
|
-
var pkg = require("../package.json");
|
|
13
|
-
|
|
14
|
-
// a sample fluentd message
|
|
15
|
-
var data = ["tag", [[1440949922, {"message": "hi there"}]]];
|
|
16
|
-
var packed = msgpack.encode(data); // 30 bytes per message
|
|
17
|
-
var packsize = packed.length;
|
|
18
|
-
var opcount = 1000000;
|
|
19
|
-
var joincount = 100;
|
|
20
|
-
var packjoin = repeatbuf(packed, joincount); // 3KB per chunk
|
|
21
|
-
var limit = 2;
|
|
22
|
-
|
|
23
|
-
var blocksToJoin = []
|
|
24
|
-
var streamForJoin = new EncoderStream();
|
|
25
|
-
streamForJoin.on("data", data => blocksToJoin.push(data));
|
|
26
|
-
for (var j = 0; j < joincount; j++) {
|
|
27
|
-
streamForJoin.write(data);
|
|
28
|
-
}
|
|
29
|
-
var packjoinWithRecords = Buffer.concat(blocksToJoin)
|
|
30
|
-
|
|
31
|
-
var argv = Array.prototype.slice.call(process.argv, 2);
|
|
32
|
-
|
|
33
|
-
if (argv[0] === "-v") {
|
|
34
|
-
console.warn(pkg.name + " " + pkg.version);
|
|
35
|
-
process.exit(0);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (argv[0] - 0) limit = argv.shift() - 0;
|
|
39
|
-
|
|
40
|
-
var list = [
|
|
41
|
-
['new EncoderStream().write(obj);', encode5],
|
|
42
|
-
['new DecoderStream().write(buf);', decode5],
|
|
43
|
-
['stream.write(msgpack.encode(obj));', encode1],
|
|
44
|
-
['stream.write(msgpack.decode(buf));', decode1],
|
|
45
|
-
['stream.write(notepack.encode(obj));', encode4],
|
|
46
|
-
['stream.write(notepack.decode(buf));', decode4],
|
|
47
|
-
['msgpack.Encoder().on("data",ondata).encode(obj);', encode2],
|
|
48
|
-
['msgpack.createDecodeStream().write(buf);', decode3],
|
|
49
|
-
['msgpack.createEncodeStream().write(obj);', encode3],
|
|
50
|
-
['msgpack.Decoder().on("data",ondata).decode(buf);', decode2],
|
|
51
|
-
// ['stream.write(Buffer.from(JSON.stringify(obj)));', stringify],
|
|
52
|
-
// ['stream.write(JSON.parse(buf));', parse]
|
|
53
|
-
];
|
|
54
|
-
|
|
55
|
-
function encode5(callback) {
|
|
56
|
-
var stream = new EncoderStream();
|
|
57
|
-
var cnt = counter(callback);
|
|
58
|
-
stream.on("data", cnt.inc);
|
|
59
|
-
stream.on("end", cnt.end);
|
|
60
|
-
for (var j = 0; j < opcount; j++) {
|
|
61
|
-
stream.write(data);
|
|
62
|
-
}
|
|
63
|
-
stream.end();
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function encode1(callback) {
|
|
67
|
-
var stream = new PassThrough();
|
|
68
|
-
var cnt = counter(callback);
|
|
69
|
-
stream.on("data", cnt.buf);
|
|
70
|
-
stream.on("end", cnt.end);
|
|
71
|
-
for (var j = 0; j < opcount; j++) {
|
|
72
|
-
stream.write(msgpack.encode(data));
|
|
73
|
-
}
|
|
74
|
-
stream.end();
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function encode2(callback) {
|
|
78
|
-
var stream = new PassThrough();
|
|
79
|
-
var cnt = counter(callback);
|
|
80
|
-
stream.on("data", cnt.buf);
|
|
81
|
-
stream.on("end", cnt.end);
|
|
82
|
-
var encoder = Encoder();
|
|
83
|
-
encoder.on("data", function(chunk) {
|
|
84
|
-
stream.write(chunk);
|
|
85
|
-
});
|
|
86
|
-
encoder.on("end", function() {
|
|
87
|
-
stream.end();
|
|
88
|
-
});
|
|
89
|
-
for (var j = 0; j < opcount; j++) {
|
|
90
|
-
encoder.encode(data);
|
|
91
|
-
}
|
|
92
|
-
encoder.end();
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function encode3(callback) {
|
|
96
|
-
var stream = msgpack.createEncodeStream();
|
|
97
|
-
var cnt = counter(callback);
|
|
98
|
-
stream.on("data", cnt.buf);
|
|
99
|
-
stream.on("end", cnt.end);
|
|
100
|
-
for (var j = 0; j < opcount; j++) {
|
|
101
|
-
stream.write(data);
|
|
102
|
-
}
|
|
103
|
-
stream.end();
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function encode4(callback) {
|
|
107
|
-
var stream = new PassThrough();
|
|
108
|
-
var cnt = counter(callback);
|
|
109
|
-
stream.on("data", cnt.buf);
|
|
110
|
-
stream.on("end", cnt.end);
|
|
111
|
-
for (var j = 0; j < opcount; j++) {
|
|
112
|
-
stream.write(notepack.encode(data));
|
|
113
|
-
}
|
|
114
|
-
stream.end();
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function decode5(callback) {
|
|
118
|
-
var stream = new DecoderStream();
|
|
119
|
-
var cnt = counter(callback);
|
|
120
|
-
stream.on("data", cnt.inc);
|
|
121
|
-
stream.on("end", cnt.end);
|
|
122
|
-
for (var j = 0; j < opcount / joincount; j++) {
|
|
123
|
-
stream.write(packjoinWithRecords);
|
|
124
|
-
}
|
|
125
|
-
stream.end();
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function decode1(callback) {
|
|
129
|
-
var stream = new PassThrough({objectMode: true});
|
|
130
|
-
var cnt = counter(callback);
|
|
131
|
-
stream.on("data", cnt.inc);
|
|
132
|
-
stream.on("end", cnt.end);
|
|
133
|
-
for (var j = 0; j < opcount; j++) {
|
|
134
|
-
stream.write(msgpack.decode(packed));
|
|
135
|
-
}
|
|
136
|
-
stream.end();
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function decode2(callback) {
|
|
140
|
-
var stream = new PassThrough({objectMode: true});
|
|
141
|
-
var cnt = counter(callback);
|
|
142
|
-
stream.on("data", cnt.inc);
|
|
143
|
-
stream.on("end", cnt.end);
|
|
144
|
-
var decoder = Decoder();
|
|
145
|
-
decoder.on("data", function(chunk) {
|
|
146
|
-
stream.write(chunk);
|
|
147
|
-
});
|
|
148
|
-
decoder.on("end", function() {
|
|
149
|
-
stream.end();
|
|
150
|
-
});
|
|
151
|
-
for (var j = 0; j < opcount / joincount; j++) {
|
|
152
|
-
decoder.decode(packjoin);
|
|
153
|
-
}
|
|
154
|
-
decoder.end();
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function decode3(callback) {
|
|
158
|
-
var stream = msgpack.createDecodeStream();
|
|
159
|
-
var cnt = counter(callback);
|
|
160
|
-
stream.on("data", cnt.inc);
|
|
161
|
-
stream.on("end", cnt.end);
|
|
162
|
-
for (var j = 0; j < opcount / joincount; j++) {
|
|
163
|
-
stream.write(packjoin);
|
|
164
|
-
}
|
|
165
|
-
stream.end();
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function decode4(callback) {
|
|
169
|
-
var stream = new PassThrough({objectMode: true});
|
|
170
|
-
var cnt = counter(callback);
|
|
171
|
-
stream.on("data", cnt.inc);
|
|
172
|
-
stream.on("end", cnt.end);
|
|
173
|
-
for (var j = 0; j < opcount; j++) {
|
|
174
|
-
stream.write(notepack.decode(packed));
|
|
175
|
-
}
|
|
176
|
-
stream.end();
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function rpad(str, len, chr) {
|
|
180
|
-
if (!chr) chr = " ";
|
|
181
|
-
str += "";
|
|
182
|
-
while (str.length < len) str += chr;
|
|
183
|
-
return str;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function lpad(str, len, chr) {
|
|
187
|
-
if (!chr) chr = " ";
|
|
188
|
-
str += "";
|
|
189
|
-
while (str.length < len) str = chr + str;
|
|
190
|
-
return str;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
function repeatbuf(buf, cnt) {
|
|
194
|
-
var array = [];
|
|
195
|
-
for (var i = 0; i < cnt; i++) {
|
|
196
|
-
array.push(buf);
|
|
197
|
-
}
|
|
198
|
-
return Buffer.concat(array);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
function counter(callback) {
|
|
202
|
-
var cnt = 0;
|
|
203
|
-
return {buf: b, inc: i, end: e};
|
|
204
|
-
|
|
205
|
-
function b(buf) {
|
|
206
|
-
cnt += buf.length / packsize;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function i() {
|
|
210
|
-
cnt++;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
function e() {
|
|
214
|
-
cnt = Math.round(cnt);
|
|
215
|
-
callback(null, cnt);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
function run() {
|
|
220
|
-
// task filter
|
|
221
|
-
if (argv.length) {
|
|
222
|
-
list = list.filter(function(pair) {
|
|
223
|
-
var name = pair[0];
|
|
224
|
-
var match = argv.filter(function(grep) {
|
|
225
|
-
return (name.indexOf(grep) > -1);
|
|
226
|
-
});
|
|
227
|
-
return match.length;
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// run tasks repeatedly
|
|
232
|
-
var tasks = [];
|
|
233
|
-
for (var i = 0; i < limit; i++) {
|
|
234
|
-
tasks.push(oneset);
|
|
235
|
-
}
|
|
236
|
-
async.series(tasks, end);
|
|
237
|
-
|
|
238
|
-
// run a series of tasks
|
|
239
|
-
function oneset(callback) {
|
|
240
|
-
async.eachSeries(list, bench, callback);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
// run a single benchmark
|
|
244
|
-
function bench(pair, callback) {
|
|
245
|
-
process.stdout.write(".");
|
|
246
|
-
var func = pair[1];
|
|
247
|
-
var start = new Date() - 0;
|
|
248
|
-
func(function(err, cnt) {
|
|
249
|
-
var end = new Date() - 0;
|
|
250
|
-
var array = pair[2] || (pair[2] = []);
|
|
251
|
-
array.push(end - start);
|
|
252
|
-
pair[3] = cnt;
|
|
253
|
-
setTimeout(callback, 100);
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// show result
|
|
258
|
-
function end() {
|
|
259
|
-
var title = "operation (" + opcount + " x " + limit + ")";
|
|
260
|
-
process.stdout.write("\n");
|
|
261
|
-
|
|
262
|
-
// table header
|
|
263
|
-
var COL1 = 48;
|
|
264
|
-
console.log(rpad(title, COL1), "|", " op ", "|", " ms ", "|", " op/s ");
|
|
265
|
-
console.log(rpad("", COL1, "-"), "|", "------:", "|", "----:", "|", "-----:");
|
|
266
|
-
|
|
267
|
-
// table body
|
|
268
|
-
list.forEach(function(pair) {
|
|
269
|
-
var name = pair[0];
|
|
270
|
-
var op = pair[3];
|
|
271
|
-
var array = pair[2];
|
|
272
|
-
array = array.sort(function(a, b) {
|
|
273
|
-
return a > b;
|
|
274
|
-
});
|
|
275
|
-
var fastest = array[0];
|
|
276
|
-
var score = Math.floor(opcount / fastest * 1000);
|
|
277
|
-
console.log(rpad(name, COL1), "|", lpad(op, 7), "|", lpad(fastest, 5), "|", lpad(score, 6));
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
run();
|
package/tests/benchmark.cjs
DELETED
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
var cborX = tryRequire("../dist/node.cjs");
|
|
2
|
-
var msgpack_node = tryRequire("msgpack");
|
|
3
|
-
var msgpack_msgpack = tryRequire("@msgpack/msgpack");
|
|
4
|
-
var msgpack_lite = tryRequire("msgpack-lite");
|
|
5
|
-
var msgpack_js = tryRequire("msgpack-js");
|
|
6
|
-
var msgpack_js_v5 = tryRequire("msgpack-js-v5");
|
|
7
|
-
var msgpack5 = tryRequire("msgpack5");
|
|
8
|
-
var msgpack_unpack = tryRequire("msgpack-unpack");
|
|
9
|
-
var msgpack_codec = tryRequire("msgpack.codec");
|
|
10
|
-
var notepack = tryRequire("notepack");
|
|
11
|
-
var what_the_pack = tryRequire("what-the-pack");
|
|
12
|
-
var avro = tryRequire('avsc')
|
|
13
|
-
var cbor = tryRequire('cbor')
|
|
14
|
-
var cborSync = tryRequire('cbor-sync')
|
|
15
|
-
var zlib = require('zlib')
|
|
16
|
-
var deflateSync = zlib.deflateSync
|
|
17
|
-
var inflateSync = zlib.inflateSync
|
|
18
|
-
//var deflateSync = zlib.brotliCompressSync
|
|
19
|
-
//var inflateSync = zlib.brotliDecompressSync
|
|
20
|
-
var constants = zlib.constants
|
|
21
|
-
|
|
22
|
-
msgpack5 = msgpack5 && msgpack5();
|
|
23
|
-
msgpack_codec = msgpack_codec && msgpack_codec.msgpack;
|
|
24
|
-
what_the_pack = what_the_pack && what_the_pack.initialize(2**20);
|
|
25
|
-
|
|
26
|
-
var pkg = require("../package.json");
|
|
27
|
-
var data = require("./example4.json");
|
|
28
|
-
var packed = msgpack_lite && msgpack_lite.encode(data);
|
|
29
|
-
var expected = JSON.stringify(data);
|
|
30
|
-
|
|
31
|
-
var argv = Array.prototype.slice.call(process.argv, 2);
|
|
32
|
-
|
|
33
|
-
if (argv[0] === "-v") {
|
|
34
|
-
console.warn(pkg.name + " " + pkg.version);
|
|
35
|
-
process.exit(0);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
var limit = 5;
|
|
39
|
-
if (argv[0] - 0) limit = argv.shift() - 0;
|
|
40
|
-
limit *= 1000;
|
|
41
|
-
|
|
42
|
-
var COL1 = 58;
|
|
43
|
-
var COL2 = 7;
|
|
44
|
-
var COL3 = 5;
|
|
45
|
-
var COL4 = 6;
|
|
46
|
-
|
|
47
|
-
console.log(rpad("operation", COL1), "|", " op ", "|", " ms ", "|", " op/s ");
|
|
48
|
-
console.log(rpad("", COL1, "-"), "|", lpad(":", COL2, "-"), "|", lpad(":", COL3, "-"), "|", lpad(":", COL4, "-"));
|
|
49
|
-
|
|
50
|
-
var buf, obj;
|
|
51
|
-
|
|
52
|
-
if (cborX) {
|
|
53
|
-
let encoder = new cborX.Encoder({ useRecords: false, pack: true })
|
|
54
|
-
buf = bench('cbor-x packed and compress: encoder.encode(obj);', (data) => deflateSync(encoder.encode(data), { level: constants.Z_BEST_SPEED}), data);
|
|
55
|
-
console.log('size', buf.length)
|
|
56
|
-
|
|
57
|
-
buf = bench('cbor-x packed: encoder.encode(obj);', encoder.encode.bind(encoder), data);
|
|
58
|
-
|
|
59
|
-
obj = bench('cbor-x packed: encoder.decode(buf);', encoder.decode.bind(encoder), buf);
|
|
60
|
-
test(obj);
|
|
61
|
-
console.log('size', buf.length)
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
encoder = new cborX.Encoder({ useRecords: true, pack: true })
|
|
65
|
-
buf = bench('cbor-x packed and useRecords and compress: encoder.encode(obj);', (data) => deflateSync(encoder.encode(data), { level: constants.Z_BEST_SPEED}), data);
|
|
66
|
-
console.log('size', buf.length)
|
|
67
|
-
|
|
68
|
-
buf = bench('cbor-x packed and useRecords: encoder.encode(obj);', encoder.encode.bind(encoder), data);
|
|
69
|
-
|
|
70
|
-
obj = bench('cbor-x packed and useRecords: encoder.decode(buf);', encoder.decode.bind(encoder), buf);
|
|
71
|
-
test(obj);
|
|
72
|
-
console.log('size', buf.length)
|
|
73
|
-
|
|
74
|
-
buf = bench('require("cbor-x").encode(obj) and compress;', (data) => deflateSync(cborX.encode(data)), data);
|
|
75
|
-
console.log('size', buf.length)
|
|
76
|
-
buf = bench('require("cbor-x").encode(obj);', cborX.encode, data);
|
|
77
|
-
// buf = bench('require("msgpack").serialize(obj);', data => {let result = packr.serialize(data); packr.resetMemory(); return result;}, data);
|
|
78
|
-
|
|
79
|
-
obj = bench('require("cbor-x").decode(buf);', cborX.decode, buf);
|
|
80
|
-
test(obj);
|
|
81
|
-
console.log('size', buf.length)
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
encoder = new cborX.Encoder({ })
|
|
85
|
-
buf = bench('cbor-x w/ shared structures: encoder.encode(obj);', (data) => deflateSync(encoder.encode(data), { level: constants.Z_BEST_SPEED}), data);
|
|
86
|
-
console.log('size', buf.length)
|
|
87
|
-
|
|
88
|
-
buf = bench('cbor-x w/ shared structures: encoder.encode(obj);', encoder.encode.bind(encoder), data);
|
|
89
|
-
|
|
90
|
-
obj = bench('cbor-x w/ shared structures: encoder.decode(buf);', encoder.decode.bind(encoder), buf);
|
|
91
|
-
test(obj);
|
|
92
|
-
console.log('size', buf.length)
|
|
93
|
-
}
|
|
94
|
-
if (JSON) {
|
|
95
|
-
buf = bench('buf = Buffer(JSON.stringify(obj));', JSON_stringify, data);
|
|
96
|
-
obj = bench('obj = JSON.parse(buf);', JSON.parse, buf);
|
|
97
|
-
test(obj);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (JSON) {
|
|
102
|
-
buf = bench('buf = Buffer(JSON.stringify(obj));', JSON_stringify, data);
|
|
103
|
-
obj = bench('obj = JSON.parse(buf);', JSON.parse, buf);
|
|
104
|
-
test(obj);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (msgpack_lite) {
|
|
108
|
-
buf = bench('buf = require("msgpack-lite").encode(obj);', msgpack_lite.encode, data);
|
|
109
|
-
obj = bench('obj = require("msgpack-lite").decode(buf);', msgpack_lite.decode, packed);
|
|
110
|
-
test(obj);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (msgpack_msgpack) {
|
|
114
|
-
buf = bench('buf = require("@msgpack/msgpack").encode(obj);', msgpack_msgpack.encode, data);
|
|
115
|
-
obj = bench('obj = require("@msgpack/msgpack").decode(buf);', msgpack_msgpack.decode, buf);
|
|
116
|
-
test(obj);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (msgpack_node) {
|
|
120
|
-
buf = bench('buf = require("msgpack").pack(obj);', msgpack_node.pack, data);
|
|
121
|
-
obj = bench('obj = require("msgpack").unpack(buf);', msgpack_node.unpack, buf);
|
|
122
|
-
test(obj);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (msgpack_codec) {
|
|
126
|
-
buf = bench('buf = Buffer(require("msgpack.codec").msgpack.pack(obj));', msgpack_codec_pack, data);
|
|
127
|
-
obj = bench('obj = require("msgpack.codec").msgpack.unpack(buf);', msgpack_codec.unpack, buf);
|
|
128
|
-
test(obj);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (msgpack_js_v5) {
|
|
132
|
-
buf = bench('buf = require("msgpack-js-v5").encode(obj);', msgpack_js_v5.encode, data);
|
|
133
|
-
obj = bench('obj = require("msgpack-js-v5").decode(buf);', msgpack_js_v5.decode, buf);
|
|
134
|
-
test(obj);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if (msgpack_js) {
|
|
138
|
-
buf = bench('buf = require("msgpack-js").encode(obj);', msgpack_js.encode, data);
|
|
139
|
-
obj = bench('obj = require("msgpack-js").decode(buf);', msgpack_js.decode, buf);
|
|
140
|
-
test(obj);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
if (msgpack5) {
|
|
144
|
-
buf = bench('buf = require("msgpack5")().encode(obj);', msgpack5.encode, data);
|
|
145
|
-
obj = bench('obj = require("msgpack5")().decode(buf);', msgpack5.decode, buf);
|
|
146
|
-
test(obj);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (notepack) {
|
|
150
|
-
buf = bench('buf = require("notepack").encode(obj);', notepack.encode, data);
|
|
151
|
-
obj = bench('obj = require("notepack").decode(buf);', notepack.decode, buf);
|
|
152
|
-
test(obj);
|
|
153
|
-
}
|
|
154
|
-
if (what_the_pack) {
|
|
155
|
-
buf = bench('require("what-the-pack")... encoder.encode(obj);', what_the_pack.encode, data);
|
|
156
|
-
obj = bench('require("what-the-pack")... encoder.decode(buf);', what_the_pack.decode, buf);
|
|
157
|
-
test(obj);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (msgpack_unpack) {
|
|
161
|
-
obj = bench('obj = require("msgpack-unpack").decode(buf);', msgpack_unpack, packed);
|
|
162
|
-
test(obj);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
if (avro) {
|
|
166
|
-
const type = avro.Type.forValue(data);
|
|
167
|
-
buf = bench('require("avsc")...make schema/type...type.toBuffer(obj);', type.toBuffer.bind(type), data);
|
|
168
|
-
obj = bench('require("avsc")...make schema/type...type.fromBuffer(obj);', type.fromBuffer.bind(type), buf);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
if (cbor) {
|
|
172
|
-
buf = bench('buf = require("cbor").encode(obj);', cbor.encode, data);
|
|
173
|
-
obj = bench('obj = require("cbor").decode(buf);', cbor.decode, buf);
|
|
174
|
-
test(obj);
|
|
175
|
-
}
|
|
176
|
-
if (cborSync) {
|
|
177
|
-
buf = bench('buf = require("cbor-sync").encode(obj);', cborSync.encode, data);
|
|
178
|
-
obj = bench('obj = require("cbor-sync").decode(buf);', cborSync.decode, buf);
|
|
179
|
-
test(obj);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function JSON_stringify(src) {
|
|
183
|
-
return Buffer(JSON.stringify(src));
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function msgpack_codec_pack(data) {
|
|
187
|
-
return Buffer(msgpack_codec.pack(data));
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function bench(name, func, src) {
|
|
191
|
-
if (argv.length) {
|
|
192
|
-
var match = argv.filter(function(grep) {
|
|
193
|
-
return (name.indexOf(grep) > -1);
|
|
194
|
-
});
|
|
195
|
-
if (!match.length) return SKIP;
|
|
196
|
-
}
|
|
197
|
-
var ret, duration;
|
|
198
|
-
var start = new Date() - 0;
|
|
199
|
-
var count = 0;
|
|
200
|
-
while (1) {
|
|
201
|
-
var end = new Date() - 0;
|
|
202
|
-
duration = end - start;
|
|
203
|
-
if (duration >= limit) break;
|
|
204
|
-
while ((++count) % 100) ret = func(src);
|
|
205
|
-
}
|
|
206
|
-
name = rpad(name, COL1);
|
|
207
|
-
var score = Math.floor(count / duration * 1000);
|
|
208
|
-
count = lpad(count, COL2);
|
|
209
|
-
duration = lpad(duration, COL3);
|
|
210
|
-
score = lpad(score, COL4);
|
|
211
|
-
console.log(name, "|", count, "|", duration, "|", score);
|
|
212
|
-
return ret;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function rpad(str, len, chr) {
|
|
216
|
-
if (!chr) chr = " ";
|
|
217
|
-
while (str.length < len) str += chr;
|
|
218
|
-
return str;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
function lpad(str, len, chr) {
|
|
222
|
-
if (!chr) chr = " ";
|
|
223
|
-
str += "";
|
|
224
|
-
while (str.length < len) str = chr + str;
|
|
225
|
-
return str;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
function test(actual) {
|
|
229
|
-
if (actual === SKIP) return;
|
|
230
|
-
actual = JSON.stringify(actual);
|
|
231
|
-
if (actual === expected) return;
|
|
232
|
-
console.warn("expected: " + expected);
|
|
233
|
-
console.warn("actual: " + actual);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
function SKIP() {
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
function tryRequire(name) {
|
|
240
|
-
try {
|
|
241
|
-
return require(name);
|
|
242
|
-
} catch (e) {
|
|
243
|
-
// ignore
|
|
244
|
-
}
|
|
245
|
-
}
|
package/tests/example.json
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"int0": 0,
|
|
3
|
-
"int1": 1,
|
|
4
|
-
"int1-": -1,
|
|
5
|
-
"int8": 255,
|
|
6
|
-
"int8-": -255,
|
|
7
|
-
"int16": 256,
|
|
8
|
-
"int16-": -256,
|
|
9
|
-
"int32": 65536,
|
|
10
|
-
"int32-": -65536,
|
|
11
|
-
"nil": null,
|
|
12
|
-
"true": true,
|
|
13
|
-
"false": false,
|
|
14
|
-
"float": 0.5,
|
|
15
|
-
"float-": -0.5,
|
|
16
|
-
"string0": "",
|
|
17
|
-
"string1": "A",
|
|
18
|
-
"string4": "foobarbaz",
|
|
19
|
-
"string8": "Omnes viae Romam ducunt.",
|
|
20
|
-
"string16": "L’homme n’est qu’un roseau, le plus faible de la nature ; mais c’est un roseau pensant. Il ne faut pas que l’univers entier s’arme pour l’écraser : une vapeur, une goutte d’eau, suffit pour le tuer. Mais, quand l’univers l’écraserait, l’homme serait encore plus noble que ce qui le tue, puisqu’il sait qu’il meurt, et l’avantage que l’univers a sur lui, l’univers n’en sait rien. Toute notre dignité consiste donc en la pensée. C’est de là qu’il faut nous relever et non de l’espace et de la durée, que nous ne saurions remplir. Travaillons donc à bien penser : voilà le principe de la morale.",
|
|
21
|
-
"array0": [],
|
|
22
|
-
"array1": [
|
|
23
|
-
"foo"
|
|
24
|
-
],
|
|
25
|
-
"array8": [
|
|
26
|
-
1,
|
|
27
|
-
2,
|
|
28
|
-
4,
|
|
29
|
-
8,
|
|
30
|
-
16,
|
|
31
|
-
32,
|
|
32
|
-
64,
|
|
33
|
-
128,
|
|
34
|
-
256,
|
|
35
|
-
512,
|
|
36
|
-
1024,
|
|
37
|
-
2048,
|
|
38
|
-
4096,
|
|
39
|
-
8192,
|
|
40
|
-
16384,
|
|
41
|
-
32768,
|
|
42
|
-
65536,
|
|
43
|
-
131072,
|
|
44
|
-
262144,
|
|
45
|
-
524288,
|
|
46
|
-
1048576
|
|
47
|
-
],
|
|
48
|
-
"map0": {},
|
|
49
|
-
"map1": {
|
|
50
|
-
"foo": "bar"
|
|
51
|
-
}
|
|
52
|
-
}
|