Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements for insertMany() #14724

Merged
merged 9 commits into from
Jul 10, 2024
24 changes: 14 additions & 10 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -3868,17 +3868,21 @@ Document.prototype.$toObject = function(options, json) {
if (hasOnlyPrimitiveValues) {
// Fast path: if we don't have any nested objects or arrays, we only need a
// shallow clone.
ret = this._doc
? options.minimize ? (minimize({ ...this._doc }) || {}) : { ...this._doc }
: {};
if (!options.minimize || options.flattenObjectIds) {
const keys = Object.keys(ret);
for (const key of keys) {
// If `minimize` is false, still need to remove undefined keys
if (!options.minimize && ret[key] === undefined) {
if (this._doc == null) {
ret = {};
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
}
ret = {};
if (this._doc != null) {
for (const key of Object.keys(this._doc)) {
const value = this._doc[key];
if (value instanceof Date) {
ret[key] = new Date(value);
} else if (value === undefined) {
delete ret[key];
} else if (options.flattenObjectIds && isBsonType(ret[key], 'ObjectId')) {
ret[key] = ret[key].toJSON();
} else if (options.flattenObjectIds && isBsonType(value, 'ObjectId')) {
ret[key] = value.toJSON();
} else {
ret[key] = value;
}
}
}
Expand Down
Loading