Is Base64 Encoded BSON Less Than BSON?

Is Base64 Encoded BSON Less Than BSON?

+2


a source to share


3 answers


Piskvor right, base64-encoded - which is more than raw. You are base64-encoding something to get it over the character's limited repertoire channel, not as a downsizing tool.

Perhaps the question should be: Is BSON encoded BSON smaller than JSON ?

If so, then JSON-vs-BSON is very content dependent. For example, arbitrary floating point numbers such as 1.2345678901234567

are more efficiently stored in 8 binary bytes in BSON than the JSON version of the string. But more common numbers, for example, 1

are much more efficiently stored as strings in JSON.



For string values, BSON loses 4 bytes for a word of length, but gets some back for each "

and \

JSON has to run, plus more on escape character strings where JSON has to use a hex sequence. (Some JSON encoders also \u

- store each non-ASCII character to ensure safe transmission regardless of the character set.)

IMO: BSON doesn't have much of an edge over JSON in general. Its strength lies in its ease of decoding in a low-level language, as well as data formats that JavaScript does not. It can have marginal benefits for binary strings and a few other cases; this is definitely worth checking for your specific workload. But this suggests that there are significantly fewer examples in the BSON specification in JSON.

As for base64 encoded BSON: same except 33% worse.

+9


a source


No: with base64, 3 bytes of plaintext becomes 4 bytes of encoded text, so the result will always be larger, no matter what the data payload is. See Also: http://en.wikipedia.org/wiki/Base64



+5


a source


just wrote this as my solution to shorten the bson please check this may help you:

var bsonShortify = {
  encode:function(bson){
    return this._hex2urlBase(bson.substr(0,bson.length/2))+this._hex2urlBase(bson.substr(bson.length/2,bson.length/2));
  },
  decode:function(token){
    return this._urlBase2hex(token.substr(0,token.length/2))+this._urlBase2hex(token.substr(token.length/2,token.length/2));
  },
  _base:62,
  _baseChars:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
  _urlBase2hex:function(token){
    var s = 0, n, l = (n = token.split("")).length, i = 0;
    while(l--) s += this._baseChars.indexOf(n[i++]) * Math.pow(this._base, l);
    return s.toString(16);
  },
  _hex2urlBase:function(bson){
    var s = "", n = parseInt(bson,16);
    while(n) s = this._baseChars[n % this._base] + s, n = Math.floor(n / this._base);
    return s;
  } 
}

      

TEST

//we have bson
var bson = '4f907f7e53a58f4313000028';
//let encode it
var urlstring = bsonShortify.encode(bson) // = OqAYQdCHijCDMbRg
//let decode urlstring
var decoded_bson = bsonShortify.decode(urlstring); // = 4f907f7e53a58f4313000028

console.log('bson',bson);
console.log('urlstring',urlstring);
console.log('decoded_bson',decoded_bson);

      

0


a source







All Articles