Skip to content

Commit 18f944d

Browse files
committed
perf(document): add a couple of small perf improvements re: #10400
1 parent 4c85c70 commit 18f944d

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

lib/document.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ Document.prototype.$__init = function(doc, opts) {
747747

748748
const hasIncludedChildren = this.$__.exclude === false && this.$__.fields ?
749749
$__hasIncludedChildren(this.$__.fields) :
750-
{};
750+
null;
751751
$__applyDefaults(this, this.$__.fields, this.$__.exclude, hasIncludedChildren, false, this.$__.skipDefaults);
752752

753753
return this;

lib/schema/documentarray.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ DocumentArrayPath.prototype.getDefault = function(scope) {
365365
return ret;
366366
};
367367

368+
const _toObjectOptions = Object.freeze({ transform: false, virtuals: false });
369+
const initDocumentOptions = Object.freeze({ skipId: true, willInit: true });
370+
368371
/**
369372
* Casts contents
370373
*
@@ -384,7 +387,7 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
384387

385388
let selected;
386389
let subdoc;
387-
const _opts = { transform: false, virtuals: false };
390+
388391
options = options || {};
389392

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

416419
const rawArray = utils.isMongooseDocumentArray(value) ? value.__array : value;
417-
418420
const len = rawArray.length;
419-
const initDocumentOptions = { skipId: true, willInit: true };
420421

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

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

0 commit comments

Comments
 (0)