Skip to content

Instantly share code, notes, and snippets.

@zamnuts
Created July 25, 2014 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zamnuts/050a99de88f4ad05ccf4 to your computer and use it in GitHub Desktop.
Save zamnuts/050a99de88f4ad05ccf4 to your computer and use it in GitHub Desktop.
MongoDB stores a String as a BSON Symbol with a fixed overhead of 39 bytes.
var bson = require('bson').BSONPure;
var str = 'привет';
var buf = new Buffer(str);
console.log({
str: str, // 'привет'
buf: buf, // <Buffer d0 bf d1 80 d0 b8 d0 b2 d0 b5 d1 82>
strLen: str.length, // 6
bufLen: buf.length, // 12
bufBytes: Buffer.byteLength(str,'utf8'), // 12
bsonStr: bson.BSON.calculateObjectSize(str), // 65
bsonBuf: bson.BSON.calculateObjectSize(buf), // 8320
bsonSymStr: bson.BSON.calculateObjectSize(bson.Symbol(str)) // 51, 51 - 39 = 12 (bingo!)
});
/* Findings:
* BSON Symbol overhead is fixed at 39 bytes.
* BSON Symbol length (minus the overhead) is the number of bytes in a string.
* MongoDB String data type is a BSON Symbol, see https://github.com/mongodb/mongo/blob/v2.6.3/src/mongo/bson/bsontypes.h#L130-L132
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment