CBOR is a data format whose design goals include the possibility of extremely small code size, fairly small message size, and extensibility without the need for version negotiation.
The API is self-explanatory:
// Initialize some dataconst json ="key": "value""another key":1 2 3"number": 3141592653589793"nulls are also encoded": null;const encoded = CBOR; // ArrayBufferconst decoded = CBOR; // Object// You can also encode into a pre-allocated bufferconst encoded = CBOR;
The library is targeted for both Node and Browsers using NPM:
> npm install cbor@npm:jprochazk/cbor
This installs the library under the
// as CommonJSconst CBOR = ;// as an ES module;CBOR;CBOR;
Or included in the page as a static script from the
Benchmark is available here.
|Chrome||5225 ops/s||8998 ops/s||9268 ops/s|
|Firefox||20454 ops/s||22323 ops/s||22900 ops/s|
Results are on a i5-8600k intel processor. Your mileage may vary. The JSON data used in the test is 2 KB decoded and 1.8 KB encoded. I specifically chose this data, because it encodes badly (lots of nested objects with only a single property). The benchmark suggests the library can encode/decode around 10 MB/s in Chrome and around 50 MB/s in Firefox. Hopefully, these are meaningful results.
There are a few things from the specification which are currently unimplemented:
- Byte strings
- Tags, and the items they represent:
- date/time, bignum/bigfloat as byte string, and others
- 16-bit float (IEEE754 binary16)
- Integers larger than 32 bit (BigInt)
If you need one or more of these features, submit an issue.