@jprochazk/cbor
Advanced tools
Comparing version 0.4.5 to 0.4.6
{ | ||
"name": "@jprochazk/cbor", | ||
"version": "0.4.5", | ||
"version": "0.4.6", | ||
"description": "Partial implementation of RFC 7049 (CBOR)", | ||
@@ -5,0 +5,0 @@ "author": "Jan Procházka", |
@@ -1,5 +0,1 @@ | ||
# cbor | ||
JavaScript implementation of the CBOR [RFC 7049](https://tools.ietf.org/html/rfc7049). | ||
[![Build Status](https://travis-ci.com/jprochazk/cbor.svg?branch=master)](https://travis-ci.com/jprochazk/cbor) | ||
@@ -9,14 +5,8 @@ [![David](https://img.shields.io/david/dev/jprochazk/cbor)](https://github.com/jprochazk/cbor/blob/master/package.json) | ||
### Usage | ||
TypeScript implementation of the Concise Binary Object Representation [RFC 7049](https://tools.ietf.org/html/rfc7049). From the [official website](http://cbor.io/): | ||
``` | ||
> npm install cbor@npm:jprochazk/cbor | ||
``` | ||
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 above command will install the library under the `cbor` alias. | ||
The API is self-explanatory: | ||
```js | ||
import CBOR from "cbor"; | ||
// Initialize some data | ||
@@ -33,9 +23,38 @@ const json = { | ||
const encoded = CBOR.encode(json); // ArrayBuffer | ||
const decoded = CBOR.decode(encoded); // { ... } | ||
const decoded = CBOR.decode(encoded); // Object | ||
// You can also encode into a pre-allocated buffer | ||
const buffer = new ArrayBuffer(4096); | ||
const encoded = CBOR.encodeInto(data, 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 | ||
<script src="https://unpkg.com/@jprochazk/cbor@0.4.6"></script> | ||
<script> | ||
// after including the script in the page, the CBOR object is available globally | ||
CBOR.encode(...); | ||
CBOR.decode(...); | ||
</script> | ||
``` | ||
### Benchmarks | ||
@@ -45,3 +64,2 @@ | ||
| Browser | CBOR.decode | CBOR.encode | CBOR.encodeInto | | ||
@@ -52,10 +70,12 @@ | :------ | :---------- | :---------- | :-------------- | | ||
Results are on a i5-8600k intel processor. The JSON data used in the test is 2 KB decoded and 1.8 KB encoded. This means the library can decode at 13 MB/s and encode at 10 MB/s in Chrome, and around 2.5~4x as much in Firefox. | ||
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). | ||
You can squeeze out a bit more performance if you use `CBOR.encodeInto` with a sufficiently large buffer. the `CBOR.encode` default is 1024 bytes, which should be enough for the vast majority of uses, but if you ever find yourself using more than that, utilize `CBOR.encodeInto`. | ||
The benchmark suggests the library can decode at 13 MB/s and encode at 10 MB/s in Chrome, and around 2.5~4x as much in Firefox. I haven't run the benchmarks in Safari, because I don't have access to any Apple computers. | ||
### Notes | ||
This library is meant for use in both Node & browsers. Minimum supported Node version is 11. | ||
Because of the ArrayBuffer allocation strategy, the CBOR.encode method is pretty much as fast as CBOR.encodeInto. However, you can still find performance gains when encoding very large objects by preallocating a much larger buffer, or when encoding very small objects (<128 bytes) by allocating a smaller buffer. | ||
Minimum supported Node version is 11. | ||
There are a few things from the specification which are currently unimplemented: | ||
@@ -62,0 +82,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
60550
86
0