I want encode a javascript object with a ArrayBuffer element into BSON and transfer it over websocket. However the serialized BSON object is 10 time bigger than it should be.
The code is something like this:
var A = {buffer: new ArrayBuffer(1024)};
bson_buffer_size = BSON.calculateObjectSize(A) // returns 9164, I am expecting ~1024
My understanding of BSON serializer must be wrong. Does anyone know how I can create a compact BSON object with a binary buffer field?
Looking over the js-bson implementation, it looks like it uses ArrayBuffer internally to generate the serialized data, but if doesn't support serializing ArrayBuffers as binary data, so instead it treats it as an object.
For instance, if you run it through a serialization loop, this is the output you get:
BSON.deserialize(BSON.serialize({buffer: new ArrayBuffer(1024)}));
// Outputs:
{
buffer: {
'0': 0,
'1': 0,
// And so on
'1023': 0,
byteLength: 1024
}
}
Why not transfer the ArrayBuffer directly via the websocket instead of encoding it? Websockets can transfer binary data directly without any extra encoding necessary. You might need to do a bit of work since you'd have to send other attributes as a separate object, but that may not be the end of the world.