Skip to content

Commit

Permalink
perf(document): add a couple of small perf improvements re: #10400
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Feb 28, 2022
1 parent 4c85c70 commit 18f944d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ Document.prototype.$__init = function(doc, opts) {

const hasIncludedChildren = this.$__.exclude === false && this.$__.fields ?
$__hasIncludedChildren(this.$__.fields) :
{};
null;
$__applyDefaults(this, this.$__.fields, this.$__.exclude, hasIncludedChildren, false, this.$__.skipDefaults);

return this;
Expand Down
9 changes: 5 additions & 4 deletions lib/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ DocumentArrayPath.prototype.getDefault = function(scope) {
return ret;
};

const _toObjectOptions = Object.freeze({ transform: false, virtuals: false });
const initDocumentOptions = Object.freeze({ skipId: true, willInit: true });

/**
* Casts contents
*
Expand All @@ -384,7 +387,7 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {

let selected;
let subdoc;
const _opts = { transform: false, virtuals: false };

options = options || {};

if (!Array.isArray(value)) {
Expand Down Expand Up @@ -414,9 +417,7 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
}

const rawArray = utils.isMongooseDocumentArray(value) ? value.__array : value;

const len = rawArray.length;
const initDocumentOptions = { skipId: true, willInit: true };

for (let i = 0; i < len; ++i) {
if (!rawArray[i]) {
Expand Down Expand Up @@ -464,7 +465,7 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
subdoc = prev.id(rawArray[i]._id);
}

if (prev && subdoc && utils.deepEqual(subdoc.toObject(_opts), rawArray[i])) {
if (prev && subdoc && utils.deepEqual(subdoc.toObject(_toObjectOptions), rawArray[i])) {
// handle resetting doc with existing id and same data
subdoc.set(rawArray[i]);
// if set() is hooked it will have no return value
Expand Down

0 comments on commit 18f944d

Please sign in to comment.