Skip to content

Commit abe97bc

Browse files
xaviergonzmbroadst
authored andcommitted
feat(serialization): support arbitrary sizes for the internal serialization buffer
* Added an option to set the min size of the serialization buffer Added an option to set the min size of the temporary serialization buffer, plus added an optimization so a buffer is not copied after serialization when not needed. * fixed code review
1 parent 4af4741 commit abe97bc

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

lib/bson/bson.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ var deserialize = require('./parser/deserializer'),
2424
* @ignore
2525
* @api private
2626
*/
27-
// Max Size
27+
// Default Max Size
2828
var MAXSIZE = 1024 * 1024 * 17;
29-
// Max Document Buffer size
29+
30+
// Current Internal Temporary Serialization Buffer
3031
var buffer = new Buffer(MAXSIZE);
3132

3233
var BSON = function() {};
@@ -38,6 +39,7 @@ var BSON = function() {};
3839
* @param {Boolean} [options.checkKeys] the serializer will check if keys are valid.
3940
* @param {Boolean} [options.serializeFunctions=false] serialize the javascript functions **(default:false)**.
4041
* @param {Boolean} [options.ignoreUndefined=true] ignore undefined fields **(default:true)**.
42+
* @param {Number} [options.minInternalBufferSize=1024*1024*17] minimum size of the internal temporary serialization buffer **(default:1024*1024*17)**.
4143
* @return {Buffer} returns the Buffer object containing the serialized object.
4244
* @api public
4345
*/
@@ -49,6 +51,13 @@ BSON.prototype.serialize = function serialize(object, options) {
4951
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
5052
var ignoreUndefined =
5153
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
54+
var minInternalBufferSize =
55+
typeof options.minInternalBufferSize === 'number' ? options.minInternalBufferSize : MAXSIZE;
56+
57+
// Resize the internal serialization buffer if needed
58+
if (buffer.length < minInternalBufferSize) {
59+
buffer = new Buffer(minInternalBufferSize);
60+
}
5261

5362
// Attempt to serialize
5463
var serializationIndex = serializer(

0 commit comments

Comments
 (0)