You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
4.3 KiB

4 years ago
  1. # notepack
  2. [![Build Status](https://travis-ci.org/coinative/notepack.svg?branch=master)](https://travis-ci.org/coinative/notepack)
  3. [![Coverage Status](https://coveralls.io/repos/github/darrachequesne/notepack/badge.svg?branch=master)](https://coveralls.io/github/darrachequesne/notepack?branch=master)
  4. A fast [Node.js](http://nodejs.org) implementation of the latest [MessagePack](http://msgpack.org) [spec](https://github.com/msgpack/msgpack/blob/master/spec.md).
  5. ## Notes
  6. * 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.
  7. * `undefined` is encoded as `fixext 1 [0, 0]`, i.e. `<Buffer d4 00 00>`
  8. * `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>`
  9. * `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>`
  10. ## Install
  11. ```
  12. npm install notepack.io
  13. ```
  14. ## Usage
  15. ```js
  16. var notepack = require('notepack.io');
  17. var encoded = notepack.encode({ foo: 'bar'}); // <Buffer 81 a3 66 6f 6f a3 62 61 72>
  18. var decoded = notepack.decode(encoded); // { foo: 'bar' }
  19. ```
  20. ## Browser
  21. A browser version of notepack is also available (7.6 kB minified)
  22. ```html
  23. <script src="https://rawgit.com/darrachequesne/notepack/master/dist/notepack.js"></script>
  24. <script>
  25. console.log(notepack.decode(notepack.encode([1, '2', new Date()])));
  26. // [1, "2", Thu Dec 08 2016 00:00:01 GMT+0100 (CET)]
  27. </script>
  28. ```
  29. ## Performance
  30. 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.
  31. The `./benchmarks/run` output on my machine is:
  32. ```
  33. $ node -v
  34. v6.9.1
  35. $ ./benchmarks/run
  36. Encoding (this will take a while):
  37. +----------------------------+-------------------+-----------------+----------------+---------------+
  38. | │ tiny │ small │ medium │ large |
  39. +----------------------------+-------------------+-----------------+----------------+---------------+
  40. | notepack │ 1,118,284 ops/sec │ 290,050 ops/sec │ 19,670 ops/sec │ 146 ops/sec |
  41. +----------------------------+-------------------+-----------------+----------------+---------------+
  42. | msgpack-js │ 116,413 ops/sec │ 29,831 ops/sec │ 3,199 ops/sec │ 57.73 ops/sec |
  43. +----------------------------+-------------------+-----------------+----------------+---------------+
  44. | msgpack-lite │ 229,063 ops/sec │ 92,016 ops/sec │ 12,620 ops/sec │ 169 ops/sec |
  45. +----------------------------+-------------------+-----------------+----------------+---------------+
  46. | JSON.stringify (to Buffer) │ 811,373 ops/sec │ 111,166 ops/sec │ 2,556 ops/sec │ 2.83 ops/sec |
  47. +----------------------------+-------------------+-----------------+----------------+---------------+
  48. Decoding (this will take a while):
  49. +--------------------------+-------------------+-----------------+----------------+---------------+
  50. | │ tiny │ small │ medium │ large |
  51. +--------------------------+-------------------+-----------------+----------------+---------------+
  52. | notepack │ 860,763 ops/sec │ 179,459 ops/sec │ 17,642 ops/sec │ 184 ops/sec |
  53. +--------------------------+-------------------+-----------------+----------------+---------------+
  54. | msgpack-js │ 522,966 ops/sec │ 132,121 ops/sec │ 12,445 ops/sec │ 179 ops/sec |
  55. +--------------------------+-------------------+-----------------+----------------+---------------+
  56. | msgpack-lite │ 566,976 ops/sec │ 114,859 ops/sec │ 9,520 ops/sec │ 156 ops/sec |
  57. +--------------------------+-------------------+-----------------+----------------+---------------+
  58. | JSON.parse (from Buffer) │ 1,177,912 ops/sec │ 277,076 ops/sec │ 18,430 ops/sec │ 38.74 ops/sec |
  59. +--------------------------+-------------------+-----------------+----------------+---------------+
  60. * Note that JSON is provided as an indicative comparison only
  61. ```
  62. ## License
  63. MIT