Skip to content

Commit

Permalink
perf(document): remove some more unnecessary object creation in Docum…
Browse files Browse the repository at this point in the history
…ent constructor

Re: #10400
  • Loading branch information
vkarpov15 committed Feb 27, 2022
1 parent 49308ee commit 1a1d3cc
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ function Document(obj, fields, skipId, options) {
if ('priorDoc' in 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 @@ -141,17 +138,15 @@ function Document(obj, fields, skipId, options) {

const hasIncludedChildren = exclude === false && fields ?
$__hasIncludedChildren(fields) :
{};
null;

if (this._doc == null) {
this.$__buildDoc(obj, fields, skipId, exclude, hasIncludedChildren, false);

// By default, defaults get applied **before** setting initial values
// Re: gh-6155
if (defaults) {
$__applyDefaults(this, fields, exclude, hasIncludedChildren, true, {
isNew: this.$isNew
});
$__applyDefaults(this, fields, exclude, hasIncludedChildren, true, null, skipId);
}
}
if (obj) {
Expand All @@ -175,9 +170,7 @@ function Document(obj, fields, skipId, options) {
this.$__.skipDefaults = options.skipDefaults;
}
} else if (defaults) {
$__applyDefaults(this, fields, exclude, hasIncludedChildren, false, options.skipDefaults, {
isNew: this.$isNew
});
$__applyDefaults(this, fields, exclude, hasIncludedChildren, false, options.skipDefaults, skipId);
}

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

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

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

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

Expand All @@ -454,9 +447,9 @@ function $__applyDefaults(doc, fields, exclude, hasIncludedChildren, isBeforeSet
}
} else if (exclude === false && fields && !included) {
const hasSubpaths = type.$isSingleNested || type.$isMongooseDocumentArray;
if (curPath in fields || (hasSubpaths && hasIncludedChildren[curPath])) {
if (curPath in fields || (hasSubpaths && hasIncludedChildren != null && hasIncludedChildren[curPath])) {
included = true;
} else if (!hasIncludedChildren[curPath]) {
} else if (hasIncludedChildren != null && !hasIncludedChildren[curPath]) {
break;
}
}
Expand Down

0 comments on commit 1a1d3cc

Please sign in to comment.