msgpackr 1.11.9 → 1.11.11
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 +1 -3
- package/benchmark.md +26 -16
- package/dist/index-no-eval.cjs +11 -11
- package/dist/index-no-eval.cjs.map +1 -1
- package/dist/index-no-eval.min.js +1 -1
- package/dist/index-no-eval.min.js.map +1 -1
- package/dist/index.js +11 -11
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/node.cjs +11 -11
- package/dist/node.cjs.map +1 -1
- package/dist/test.js +11 -11
- package/dist/test.js.map +1 -1
- package/dist/unpack-no-eval.cjs +11 -11
- package/dist/unpack-no-eval.cjs.map +1 -1
- package/index.d.cts +2 -2
- package/index.d.ts +2 -2
- package/package.json +1 -1
- package/unpack.js +11 -11
package/README.md
CHANGED
|
@@ -7,8 +7,6 @@
|
|
|
7
7
|
[](README.md)
|
|
8
8
|
[](LICENSE)
|
|
9
9
|
|
|
10
|
-
<img align="right" src="./assets/performance.png" width="380"/>
|
|
11
|
-
|
|
12
10
|
The msgpackr package is an extremely fast MessagePack NodeJS/JavaScript implementation. Currently, it is significantly faster than any other known implementations, faster than Avro (for JS), and generally faster than native V8 JSON.stringify/parse, on NodeJS. It also includes an optional record extension (the `r` in msgpackr), for defining record structures that makes MessagePack even faster and more compact, often over twice as fast as even native JSON functions, several times faster than other JS implementations, and 15-50% more compact. See the performance section for more details. Structured cloning (with support for cyclical references) is also supported through optional extensions.
|
|
13
11
|
|
|
14
12
|
## Basic Usage
|
|
@@ -104,7 +102,7 @@ packr.pack(bigDataWithLotsOfObjects);
|
|
|
104
102
|
|
|
105
103
|
Another way to further leverage the benefits of the msgpackr record structures is to use streams that naturally allow for data to reuse based on previous record structures. The stream classes have the record structure extension enabled by default and provide excellent out-of-the-box performance.
|
|
106
104
|
|
|
107
|
-
When creating a new `Packr`, `Unpackr`, `PackrStream`, or `UnpackrStream` instance, we can enable or disable the record structure extension with the `useRecords` property. When this is `false`, the record structure extension will be disabled (standard/compatibility mode), and all objects will revert to being serialized using
|
|
105
|
+
When creating a new `Packr`, `Unpackr`, `PackrStream`, or `UnpackrStream` instance, we can enable or disable the record structure extension with the `useRecords` property. When this is `false`, the record structure extension will be disabled (standard/compatibility mode), and all objects will revert to being serialized using MessagePack `map`s, and all `map`s will be deserialized to JS `Object`s as properties (like the standalone `pack` and `unpack` functions).
|
|
108
106
|
|
|
109
107
|
Streaming with record structures works by encoding a structure the first time it is seen in a stream and referencing the structure in later messages that are sent across that stream. When an encoder can expect a decoder to understand previous structure references, this can be configured using the `sequential: true` flag, which is auto-enabled by streams, but can also be used with Packr instances.
|
|
110
108
|
|
package/benchmark.md
CHANGED
|
@@ -1,24 +1,34 @@
|
|
|
1
1
|
Here are more comprehensive benchmarks. This is comparison with the next fastest JS projects using the benchmark tool from `msgpack-lite` (and data is from some clinical research data we use that has a good mix of different value types and structures). It also includes comparison to V8 native JSON functionality, and JavaScript Avro (`avsc`, a very optimized Avro implementation):
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
require("msgpackr").
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
require("
|
|
16
|
-
require("
|
|
17
|
-
require("
|
|
18
|
-
require("
|
|
3
|
+
---------------------------------------------------------- | ------: | ----: | -----: | -----:
|
|
4
|
+
msgpackr w/ shared structures: packr.pack(obj); | 254700 | 5001 | 50929
|
|
5
|
+
msgpackr w/ shared structures: packr.unpack(buf); | 711700 | 5000 | 142340
|
|
6
|
+
require("msgpackr").pack(obj); | 234000 | 5000 | 46800
|
|
7
|
+
require("msgpackr").unpack(buf); | 186500 | 5000 | 37300
|
|
8
|
+
buf = Buffer(JSON.stringify(obj)); | 297900 | 5000 | 59580
|
|
9
|
+
obj = JSON.parse(buf); | 216600 | 5001 | 43311
|
|
10
|
+
buf = require("msgpack-lite").encode(obj); | 114000 | 5001 | 22795
|
|
11
|
+
obj = require("msgpack-lite").decode(buf); | 40700 | 5006 | 8130
|
|
12
|
+
buf = require("@msgpack/msgpack").encode(obj); | 166100 | 5000 | 33220
|
|
13
|
+
obj = require("@msgpack/msgpack").decode(buf); | 136500 | 5002 | 27289
|
|
14
|
+
buf = require("msgpack-js-v5").encode(obj); | 41600 | 5000 | 8320
|
|
15
|
+
obj = require("msgpack-js-v5").decode(buf); | 70200 | 5004 | 14028
|
|
16
|
+
buf = require("msgpack-js").encode(obj); | 40400 | 5012 | 8060
|
|
17
|
+
obj = require("msgpack-js").decode(buf); | 67100 | 5002 | 13414
|
|
18
|
+
buf = require("msgpack5")().encode(obj); | 15800 | 5024 | 3144
|
|
19
|
+
obj = require("msgpack5")().decode(buf); | 30600 | 5004 | 6115
|
|
20
|
+
buf = require("notepack").encode(obj); | 125100 | 5002 | 25009
|
|
21
|
+
obj = require("notepack").decode(buf); | 98600 | 5004 | 19704
|
|
22
|
+
require("what-the-pack")... encoder.encode(obj); | 150300 | 5001 | 30053
|
|
23
|
+
require("what-the-pack")... encoder.decode(buf); | 100100 | 5000 | 20020
|
|
24
|
+
obj = require("msgpack-unpack").decode(buf); | 14900 | 5031 | 2961
|
|
25
|
+
require("avsc")...make schema/type...type.toBuffer(obj); | 266600 | 5000 | 53320
|
|
26
|
+
require("avsc")...make schema/type...type.fromBuffer(obj); | 370200 | 5000 | 74040
|
|
19
27
|
|
|
20
28
|
(`avsc` is schema-based and more comparable in style to msgpackr with shared structures).
|
|
21
29
|
|
|
30
|
+
(note that benchmarks below are several years old)
|
|
31
|
+
|
|
22
32
|
Here is a benchmark of streaming data (again borrowed from `msgpack-lite`'s benchmarking), where msgpackr is able to take advantage of the structured record extension and really pull away from other tools:
|
|
23
33
|
|
|
24
34
|
operation (1000000 x 2) | op | ms | op/s
|
package/dist/index-no-eval.cjs
CHANGED
|
@@ -31,13 +31,6 @@
|
|
|
31
31
|
var inlineObjectReadThreshold = 2;
|
|
32
32
|
var readStruct;
|
|
33
33
|
var BlockedFunction; // we use search and replace to change the next call to BlockedFunction to avoid CSP issues for
|
|
34
|
-
// no-eval build
|
|
35
|
-
try {
|
|
36
|
-
new BlockedFunction ('');
|
|
37
|
-
} catch(error) {
|
|
38
|
-
// if eval variants are not supported, do not create inline object readers ever
|
|
39
|
-
inlineObjectReadThreshold = Infinity;
|
|
40
|
-
}
|
|
41
34
|
|
|
42
35
|
class Unpackr {
|
|
43
36
|
constructor(options) {
|
|
@@ -503,11 +496,18 @@
|
|
|
503
496
|
function readObject() {
|
|
504
497
|
// This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
|
|
505
498
|
if (readObject.count++ > inlineObjectReadThreshold) {
|
|
506
|
-
let
|
|
507
|
-
|
|
499
|
+
let optimizedReadObject;
|
|
500
|
+
try {
|
|
501
|
+
optimizedReadObject = structure.read = (new BlockedFunction ('r', 'return function(){return ' + (currentUnpackr.freezeData ? 'Object.freeze' : '') +
|
|
502
|
+
'({' + structure.map(key => key === '__proto__' ? '__proto_:r()' : validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '})}'))(read);
|
|
503
|
+
} catch(error) {
|
|
504
|
+
// in CF workers, the new BlockedFunction call could begin to fail at any point in time
|
|
505
|
+
inlineObjectReadThreshold = Infinity; // disable going forward
|
|
506
|
+
return readObject(); // recursively try again
|
|
507
|
+
}
|
|
508
508
|
if (structure.highByte === 0)
|
|
509
509
|
structure.read = createSecondByteReader(firstId, structure.read);
|
|
510
|
-
return
|
|
510
|
+
return optimizedReadObject() // second byte is already read, if there is one so immediately read object
|
|
511
511
|
}
|
|
512
512
|
let object = {};
|
|
513
513
|
for (let i = 0, l = structure.length; i < l; i++) {
|
|
@@ -977,7 +977,7 @@
|
|
|
977
977
|
if (length <= 40) {
|
|
978
978
|
let out = view.getBigUint64(start);
|
|
979
979
|
for (let i = start + 8; i < end; i += 8) {
|
|
980
|
-
out <<= BigInt(
|
|
980
|
+
out <<= BigInt(64);
|
|
981
981
|
out |= view.getBigUint64(i);
|
|
982
982
|
}
|
|
983
983
|
return out
|