开发者

Is Base64 Encoded BSON smaller then BSON?

开发者 https://www.devze.com 2022-12-27 15:40 出处:网络
Is Base64 Encoded BSON smaller then开发者_如何学编程 BSON? Piskvor\'s right, base64-encoded-anything is longer than raw. You base64-encode something to get it down a channel with a limited character r

Is Base64 Encoded BSON smaller then开发者_如何学编程 BSON?


Piskvor's right, base64-encoded-anything is longer than raw. You base64-encode something to get it down a channel with a limited character repertoire, not as a means of reducing size.

Perhaps the question should be: Is Base64-encoded BSON smaller then JSON?

If so then JSON-vs-BSON is very much dependent on the content. For example arbitrary floating point numbers like 1.2345678901234567 are more efficiently stored in 8 binary bytes in BSON than the JSON string digit version. But the more common numbers like, say, 1, are much more efficiently stored as strings in JSON.

For string values, BSON loses 4 bytes for a length word, but gets some back for every " and \ JSON has to escape, plus more in strings with control characters where JSON has to use a hex sequence. (Some JSON encoders also \u-escape every non-ASCII character to ensure safe transmission regardless of character set.)

IMO: BSON does not have a big compactness advantage over JSON in general. Its strength lies more in simplicity of decoding in a low-level language, plus datatypes JavaScript doesn't have. It can have marginal advantages for binary strings and a few other cases; it's certainly worth checking for a particular workload. But it's telling that the examples in the BSON specification itself are considerably smaller in JSON.

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


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


just wrote this as my soolution to shortify bson please check, it can 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's encode it
var urlstring = bsonShortify.encode(bson) // = OqAYQdCHijCDMbRg
//let's decode urlstring
var decoded_bson = bsonShortify.decode(urlstring); // = 4f907f7e53a58f4313000028

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

精彩评论

暂无评论...
验证码 换一张
取 消