Skip to content

Commit

Permalink
perf: reduce some more unnecessary property instantiations when creat…
Browse files Browse the repository at this point in the history
…ing a new document

Re: #10400
  • Loading branch information
vkarpov15 committed Dec 19, 2021
1 parent 8a3c69a commit e43ef16
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
6 changes: 4 additions & 2 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,6 @@ Document.prototype.$init = function() {

Document.prototype.$__init = function(doc, opts) {
this.$isNew = false;
this.$init = true;
opts = opts || {};

// handle docs with populated paths
Expand Down Expand Up @@ -988,8 +987,11 @@ Document.prototype.$session = function $session(session) {
'called `endSession()` on the session you are passing to `$session()`.');
}

this.$__.session = session;
if (session == null && this.$__.session == null) {
return;
}

this.$__.session = session;

if (!this.$isSubdocument) {
const subdocs = this.$getAllSubdocs();
Expand Down
7 changes: 3 additions & 4 deletions lib/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ module.exports = exports = InternalCache;

function InternalCache() {
this.activePaths = new ActiveRoster;

// embedded docs
this.ownerDocument = undefined;
this.fullPath = undefined;
this.strictMode = undefined;
}

InternalCache.prototype.fullPath = undefined;
InternalCache.prototype.strictMode = undefined;
InternalCache.prototype.selected = undefined;
InternalCache.prototype.shardval = undefined;
Expand All @@ -28,6 +26,7 @@ InternalCache.prototype.inserting = undefined;
InternalCache.prototype.saving = undefined;
InternalCache.prototype.version = undefined;
InternalCache.prototype._id = undefined;
InternalCache.prototype.ownerDocument = undefined;
InternalCache.prototype.populate = undefined; // what we want to populate in this doc
InternalCache.prototype.populated = undefined;// the _ids that have been populated
InternalCache.prototype.wasPopulated = false; // if this doc was the result of a population
Expand Down
16 changes: 8 additions & 8 deletions lib/queryhelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,16 @@ exports.createModel = function createModel(model, doc, fields, userProvidedField
return new discriminator(undefined, _fields, true);
}
}
if (typeof options === 'undefined') {
options = {};
options.defaults = true;
}
return new model(undefined, fields, {

const _opts = {
skipId: true,
isNew: false,
willInit: true,
defaults: options.defaults
});
willInit: true
};
if (options != null && 'defaults' in options) {
_opts.defaults = options.defaults;
}
return new model(undefined, fields, _opts);
};

/*!
Expand Down
5 changes: 4 additions & 1 deletion lib/types/subdocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ module.exports = Subdocument;
function Subdocument(value, fields, parent, skipId, options) {
if (parent != null) {
// If setting a nested path, should copy isNew from parent re: gh-7048
const parentOptions = { isNew: parent.isNew, defaults: 'defaults' in parent.$__ ? parent.$__.defaults : true };
const parentOptions = { isNew: parent.isNew };
if ('defaults' in parent.$__) {
parentOptions.defaults = parent.$__.defaults;
}
options = Object.assign({}, parentOptions, options);
}
if (options != null && options.path != null) {
Expand Down

0 comments on commit e43ef16

Please sign in to comment.