1.0.0 • Published 3 years ago

@bitcomposer/notepack.io v1.0.0

Weekly downloads
29
License
MIT
Repository
github
Last release
3 years ago

notepack

Build Status Coverage Status

A fast Node.js implementation of the latest MessagePack spec. Forked from notepack.io as we need to be able to transport RegExp and mongo ObjectId's between our Moleculer microservices.

Notes

  • This implementation is not backwards compatible with those that use the older spec. It is recommended that this library is only used in isolated systems.
  • undefined is encoded as fixext 1 [0, 0], i.e. <Buffer d4 00 00>
  • Date objects are encoded as fixext 8 [0, ms], e.g. new Date('2000-06-13T00:00:00.000Z') => <Buffer d7 00 00 00 00 df b7 62 9c 00>
  • ArrayBuffer are encoded as ext 8/16/32 [0, data], e.g. Uint8Array.of(1, 2, 3, 4) => <Buffer c7 04 00 01 02 03 04>
  • ObjectId are encoded as string 8 [0, data], e.g. new ObjectId('aaaaaaaaaaaaaaaaaaaaaaaa') => <Buffer e4 18 61 61 61 61 61 61 61 61 61 61 61 61>
  • RegExp are encoded as string 8 [0, data], e.g. new RegExp('aaa') => <Buffer e4 05 2f 61 61 61 2f> or string 16 [00, data] => <Buffer e5 01 02 2f ... 2f (256 values in ...)

Install

npm install @bitcomposer/notepack.io

Usage

var notepack = require("@bitcomposer/notepack.io");

var encoded = notepack.encode({ foo: "bar" }); // <Buffer 81 a3 66 6f 6f a3 62 61 72>
var decoded = notepack.decode(encoded); // { foo: 'bar' }

Browser

A browser version of notepack is also available (2.0 kB minified/gzipped)

ObjectId has not been included due to the heavy dependencies.

<script src="https://unpkg.com/@bitcomposer/notepack.io@1.0.0/dist/notepack.min.js"></script>
<script>
  console.log(notepack.decode(notepack.encode([1, "2", new Date()])));
  // [1, "2", Thu Dec 08 2016 00:00:01 GMT+0100 (CET)]
</script>

Performance

Performance is currently comparable to msgpack-node (which presumably needs optimizing and suffers from JS-native overhead) and is significantly faster than other implementations. Several micro-optimizations are used to improve the performance of short string and Buffer operations.

The ./benchmarks/run output on my machine is:

$ node -v
v12.15.0
$ ./benchmarks/run
Encoding (this will take a while):
+----------------------------+-------------------+-----------------+----------------+---------------+
|                            │ tiny              │ small           │ medium         │ large         |
+----------------------------+-------------------+-----------------+----------------+---------------+
| notepack                   │ 2,187,481 ops/sec │ 510,581 ops/sec │ 39,187 ops/sec │ 231 ops/sec   |
+----------------------------+-------------------+-----------------+----------------+---------------+
| msgpack-js                 │ 111,209 ops/sec   │ 95,346 ops/sec  │ 9,896 ops/sec  │ 121 ops/sec   |
+----------------------------+-------------------+-----------------+----------------+---------------+
| msgpack-lite               │ 524,993 ops/sec   │ 195,466 ops/sec │ 18,269 ops/sec │ 242 ops/sec   |
+----------------------------+-------------------+-----------------+----------------+---------------+
| @msgpack/msgpack           │ 723,885 ops/sec   │ 292,447 ops/sec │ 30,438 ops/sec │ 80.26 ops/sec |
+----------------------------+-------------------+-----------------+----------------+---------------+
| JSON.stringify (to Buffer) │ 1,359,120 ops/sec │ 335,024 ops/sec │ 15,721 ops/sec │ 25.97 ops/sec |
+----------------------------+-------------------+-----------------+----------------+---------------+
Decoding (this will take a while):
+--------------------------+-------------------+-----------------+----------------+---------------+
|                          │ tiny              │ small           │ medium         │ large         |
+--------------------------+-------------------+-----------------+----------------+---------------+
| notepack                 │ 3,165,012 ops/sec │ 642,348 ops/sec │ 32,173 ops/sec │ 249 ops/sec   |
+--------------------------+-------------------+-----------------+----------------+---------------+
| msgpack-js               │ 1,255,151 ops/sec │ 280,944 ops/sec │ 24,396 ops/sec │ 243 ops/sec   |
+--------------------------+-------------------+-----------------+----------------+---------------+
| msgpack-lite             │ 667,059 ops/sec   │ 144,927 ops/sec │ 11,922 ops/sec │ 175 ops/sec   |
+--------------------------+-------------------+-----------------+----------------+---------------+
| @msgpack/msgpack         │ 1,760,026 ops/sec │ 353,698 ops/sec │ 18,816 ops/sec │ 45.68 ops/sec |
+--------------------------+-------------------+-----------------+----------------+---------------+
| JSON.parse (from Buffer) │ 1,750,845 ops/sec │ 407,212 ops/sec │ 24,999 ops/sec │ 35.77 ops/sec |
+--------------------------+-------------------+-----------------+----------------+---------------+
* Note that JSON is provided as an indicative comparison only

License

MIT