dignifiedquire / borc

Assimilate your JavaScript objects into cbor
https://dignifiedquire.github.io/borc
MIT License
31 stars 22 forks source link

performance #17

Closed rynop closed 7 years ago

rynop commented 7 years ago

Have you happen to have done any performance analysis of using this lib to encode/decode cbor vs encoding/decoing JSON?

Similar to what Paul Irish did for cbor in browser https://github.com/paroga/cbor-js/issues/11

If it has not already been done, I'm happy to write tests.

rynop commented 7 years ago

I ended up just writing a test. borc vs https://github.com/hildjj/node-cbor vs JSON.

JSON encode/decode is significantly faster. Is this just because the cbor encode/decode is happening in pure JS? If not, what is the benefit/selling point of using cbor?

If cbor is not required, and if binary xfer is not required, what is the benefit of cbor when working in IoT environment? If working in IoT, I would think most data payloads would already be small, so size of JSON vs cbor is not a factor IMO.

Output:

~ $ node perf.js
cbor: 5.976ms
cbor: 0.625ms
cbor: 2.188ms
cbor: 0.603ms
cbor: 4.759ms
cbor: 0.598ms
cbor: 2.905ms
cbor: 3.946ms

borc: 4.721ms
borc: 0.442ms
borc: 0.332ms
borc: 0.163ms
borc: 0.318ms
borc: 0.252ms
borc: 0.201ms
borc: 1.284ms

JSON: 0.035ms
JSON: 0.013ms
JSON: 0.053ms
JSON: 0.017ms
JSON: 0.015ms
JSON: 0.080ms
JSON: 0.012ms
JSON: 0.014ms
dignifiedquire commented 7 years ago

Thank you, if you check the benchmarks they compare to JSON.stringify and parse as well

rynop commented 7 years ago

fwiw iMac 5k got following results on the benchmarks included in this repo. Needed to add benchmark dev dependency in PR above.

~/projects/borc (master|✔) $ npm run bench

> borc@2.0.2 bench /Users/ryan/projects/borc
> node benchmarks/index

PID: 34298
encode - node-cbor - 74 x 433 ops/sec ±3.47% (64 runs sampled)
encode - borc - 74 x 12,970 ops/sec ±1.66% (91 runs sampled)
encode - stream - borc - 74 x 4,781 ops/sec ±6.34% (80 runs sampled)
encode - JSON.stringify - 74 x 36,658 ops/sec ±6.35% (81 runs sampled)
decode - node-cbor - 45 x 556 ops/sec ±2.79% (81 runs sampled)
decode - borc - 45 x 17,192 ops/sec ±4.97% (80 runs sampled)
decode - JSON.parse - 45 x 47,713 ops/sec ±15.52% (54 runs sampled)