https://github.com/jprochazk/cbor
TypeScript implementation of the CBOR specification
https://github.com/jprochazk/cbor
cbor javascript json typescript
Last synced: 3 months ago
JSON representation
TypeScript implementation of the CBOR specification
- Host: GitHub
- URL: https://github.com/jprochazk/cbor
- Owner: jprochazk
- License: mit
- Created: 2020-06-25T13:20:10.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-02-10T23:12:07.000Z (over 3 years ago)
- Last Synced: 2025-09-21T20:25:10.179Z (9 months ago)
- Topics: cbor, javascript, json, typescript
- Language: TypeScript
- Homepage:
- Size: 211 KB
- Stars: 22
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.com/jprochazk/cbor)
[](https://github.com/jprochazk/cbor/blob/master/package.json)
[](https://github.com/jprochazk/cbor/blob/master/LICENSE)
TypeScript implementation of the Concise Binary Object Representation [RFC 7049](https://tools.ietf.org/html/rfc7049). From the [official website](http://cbor.io/):
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:
```js
// Initialize some data
const json = {
"key": "value",
"another key": [
1, 2, 3
],
"number": 3.141592653589793
"nulls are also encoded": null
};
const encoded = CBOR.encode(json); // ArrayBuffer
const decoded = CBOR.decode(encoded); // Object
// You can also encode into a pre-allocated buffer
const encoded = CBOR.encodeInto(new ArrayBuffer(4096), data);
```
### Usage
The library is targeted for both Node and Browsers using NPM:
```
> npm install cbor@npm:jprochazk/cbor
```
This installs the library under the `cbor` alias.
```js
// as CommonJS
const CBOR = require("cbor");
// as an ES module
import CBOR from "cbor";
CBOR.encode(...);
CBOR.decode(...);
```
Or included in the page as a static script from the `unkpg` CDN:
```html
// after including the script in the page, the CBOR object is available globally
CBOR.encode(...);
CBOR.decode(...);
```
### Benchmarks
Benchmark is available [here](https://jsbench.me/krkdop8101/1).
| Browser | CBOR.decode | CBOR.encode | CBOR.encodeInto |
| :------ | :---------- | :---------- | :-------------- |
| 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.
### Notes
There are a few things from the specification which are currently unimplemented:
- 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](https://github.com/jprochazk/cbor/issues).