Skip to content

Commit

Permalink
feat(serialization): support arbitrary sizes for the internal seriali…
Browse files Browse the repository at this point in the history
…zation 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
  • Loading branch information
xaviergonz authored and mbroadst committed Mar 12, 2018
1 parent 4af4741 commit abe97bc
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/bson/bson.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ var deserialize = require('./parser/deserializer'),
* @ignore
* @api private
*/
// Max Size
// Default Max Size
var MAXSIZE = 1024 * 1024 * 17;
// Max Document Buffer size

// Current Internal Temporary Serialization Buffer
var buffer = new Buffer(MAXSIZE);

var BSON = function() {};
Expand All @@ -38,6 +39,7 @@ var BSON = function() {};
* @param {Boolean} [options.checkKeys] the serializer will check if keys are valid.
* @param {Boolean} [options.serializeFunctions=false] serialize the javascript functions **(default:false)**.
* @param {Boolean} [options.ignoreUndefined=true] ignore undefined fields **(default:true)**.
* @param {Number} [options.minInternalBufferSize=1024*1024*17] minimum size of the internal temporary serialization buffer **(default:1024*1024*17)**.
* @return {Buffer} returns the Buffer object containing the serialized object.
* @api public
*/
Expand All @@ -49,6 +51,13 @@ BSON.prototype.serialize = function serialize(object, options) {
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
var ignoreUndefined =
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
var minInternalBufferSize =
typeof options.minInternalBufferSize === 'number' ? options.minInternalBufferSize : MAXSIZE;

// Resize the internal serialization buffer if needed
if (buffer.length < minInternalBufferSize) {
buffer = new Buffer(minInternalBufferSize);
}

// Attempt to serialize
var serializationIndex = serializer(
Expand Down

0 comments on commit abe97bc

Please sign in to comment.