Skip to content

Commit

Permalink
perf(document): avoid creating unnecessary _id for array subdocuments
Browse files Browse the repository at this point in the history
Re: #10400
  • Loading branch information
vkarpov15 committed Feb 27, 2022
1 parent b62ea22 commit 4c85c70
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ function Document(obj, fields, skipId, options) {
this.$__.priorDoc = options.priorDoc;
}

if (skipId) {
this.$__.skipId = skipId;
}

if (obj != null && typeof obj !== 'object') {
throw new ObjectParameterError(obj, 'obj', 'Document');
}
Expand Down Expand Up @@ -146,7 +150,7 @@ function Document(obj, fields, skipId, options) {
// By default, defaults get applied **before** setting initial values
// Re: gh-6155
if (defaults) {
$__applyDefaults(this, fields, exclude, hasIncludedChildren, true, null, skipId);
$__applyDefaults(this, fields, exclude, hasIncludedChildren, true, null);
}
}
if (obj) {
Expand All @@ -170,7 +174,7 @@ function Document(obj, fields, skipId, options) {
this.$__.skipDefaults = options.skipDefaults;
}
} else if (defaults) {
$__applyDefaults(this, fields, exclude, hasIncludedChildren, false, options.skipDefaults, skipId);
$__applyDefaults(this, fields, exclude, hasIncludedChildren, false, options.skipDefaults);
}

this.$__._id = this._id;
Expand Down Expand Up @@ -415,7 +419,7 @@ Object.defineProperty(Document.prototype, '$op', {
* ignore
*/

function $__applyDefaults(doc, fields, exclude, hasIncludedChildren, isBeforeSetters, pathsToSkip, skipId) {
function $__applyDefaults(doc, fields, exclude, hasIncludedChildren, isBeforeSetters, pathsToSkip) {
const paths = Object.keys(doc.$__schema.paths);
const plen = paths.length;

Expand All @@ -424,7 +428,7 @@ function $__applyDefaults(doc, fields, exclude, hasIncludedChildren, isBeforeSet
let curPath = '';
const p = paths[i];

if (p === '_id' && skipId) {
if (p === '_id' && doc.$__.skipId) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/types/ArraySubdocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function ArraySubdocument(obj, parentArr, skipId, fields, index) {
this.$setIndex(index);
this.$__parent = this[documentArrayParent];

Subdocument.call(this, obj, fields, this[documentArrayParent], void 0, { isNew: true });
Subdocument.call(this, obj, fields, this[documentArrayParent], skipId, { isNew: true });
}

/*!
Expand Down

0 comments on commit 4c85c70

Please sign in to comment.