Base64 编码的 BSON 比 BSON 小吗?
3 回答
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.
否:使用 base64,3 字节的明文变成 4 字节的编码文本,因此无论数据负载是什么,结果总是更大。另见:http ://en.wikipedia.org/wiki/Base64
刚刚写了这个作为我缩短 bson 的解决方案,请检查,它可以帮助你:
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;
}
}
测试
//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);