DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for JSON VS CBOR (Javascript)
ViperT
ViperT

Posted on

JSON VS CBOR (Javascript)

You may have had the need "serialize" some javascript object or had to use JSON to perform a deep comparison between two version of the same object, probably, yes, it was good.

But as always, good is the enemy of great, and when it comes to compare, deep clone, store, encrypt, or compress data, you need to use a Buffer (A chain of bytes if you want, mostly used as unsigned integer between 0-255) and it can be achieved in JSON after using it's stringify function by using a text encoder or a text decoder, depending on the direction of the results...

JS strings are UTF-8 always but they can be encoded into bytes by kind of looking at a table and it write the index of char into the list of number that compose the string, then it can be used in binary or in radix 10 mathematics (Binary is radix/base 2) but it's kind of a slightly more complex than explained here, yet, I will let everyone dig for the true version of how UTF-8 String are encoded and decoded using "new TextEncoder().encoder" and "new TextDecoder().decoder" ...

Ok let's resume, we convert object into a fat text and then encode the fat text into a fat list of numeral values in base/radix 2, so in other words, a buffer with binary values??? YES! That's however such a heavy process not much efficient, we increase weight and do non-willful operation...

Concise Binary Object Representation (CBOR) is a binary data serialization format loosely based on JSON authored by C. Bormann. Like JSON it allows the transmission of data objects that contain name–value pairs, but in a more concise manner. This increases processing and transfer speeds at the cost of human readability.

It is particularly interesting when we convert base64 into an array of values from 0 - 255 which produce a buffer in certain ways and then works with typed array (fixed quite clamped array much fast very kool)

It is much faster to use CBOR than JSON if you perform mostly TypedArray, Base64 (encode/decode it) serialization, and that's it!

Top comments (1)

Collapse
 
adam_cyclones profile image
Adam Crockett

Oh this is so nerdy it's right up my street thank you I'll add to my bookmarks!

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.