Skip to content

Commit 772f665

Browse files
committed
perf(document): avoid unnecessary set creation in $__reset() re: #14719
1 parent 633e4f4 commit 772f665

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

lib/document.js

+23-21
Original file line numberDiff line numberDiff line change
@@ -3470,31 +3470,33 @@ Document.prototype.$__reset = function reset() {
34703470
let _this = this;
34713471

34723472
// Skip for subdocuments
3473-
const subdocs = this.$parent() === this ? this.$getAllSubdocs() : [];
3474-
const resetArrays = new Set();
3475-
for (const subdoc of subdocs) {
3476-
const fullPathWithIndexes = subdoc.$__fullPathWithIndexes();
3477-
subdoc.$__reset();
3478-
if (this.isModified(fullPathWithIndexes) || isParentInit(fullPathWithIndexes)) {
3479-
if (subdoc.$isDocumentArrayElement) {
3480-
resetArrays.add(subdoc.parentArray());
3481-
} else {
3482-
const parent = subdoc.$parent();
3483-
if (parent === this) {
3484-
this.$__.activePaths.clearPath(subdoc.$basePath);
3485-
} else if (parent != null && parent.$isSubdocument) {
3486-
// If map path underneath subdocument, may end up with a case where
3487-
// map path is modified but parent still needs to be reset. See gh-10295
3488-
parent.$__reset();
3473+
const subdocs = !this.$isSubdocument ? this.$getAllSubdocs() : null;
3474+
if (subdocs && subdocs.length > 0) {
3475+
const resetArrays = new Set();
3476+
for (const subdoc of subdocs) {
3477+
const fullPathWithIndexes = subdoc.$__fullPathWithIndexes();
3478+
subdoc.$__reset();
3479+
if (this.isModified(fullPathWithIndexes) || isParentInit(fullPathWithIndexes)) {
3480+
if (subdoc.$isDocumentArrayElement) {
3481+
resetArrays.add(subdoc.parentArray());
3482+
} else {
3483+
const parent = subdoc.$parent();
3484+
if (parent === this) {
3485+
this.$__.activePaths.clearPath(subdoc.$basePath);
3486+
} else if (parent != null && parent.$isSubdocument) {
3487+
// If map path underneath subdocument, may end up with a case where
3488+
// map path is modified but parent still needs to be reset. See gh-10295
3489+
parent.$__reset();
3490+
}
34893491
}
34903492
}
34913493
}
3492-
}
34933494

3494-
for (const array of resetArrays) {
3495-
this.$__.activePaths.clearPath(array.$path());
3496-
array[arrayAtomicsBackupSymbol] = array[arrayAtomicsSymbol];
3497-
array[arrayAtomicsSymbol] = {};
3495+
for (const array of resetArrays) {
3496+
this.$__.activePaths.clearPath(array.$path());
3497+
array[arrayAtomicsBackupSymbol] = array[arrayAtomicsSymbol];
3498+
array[arrayAtomicsSymbol] = {};
3499+
}
34983500
}
34993501

35003502
function isParentInit(path) {

lib/model.js

+1
Original file line numberDiff line numberDiff line change
@@ -3209,6 +3209,7 @@ Model.$__insertMany = function(arr, options, callback) {
32093209
return callback(err);
32103210
}
32113211
}
3212+
32123213
if (options.session != null) {
32133214
doc.$session(options.session);
32143215
}

0 commit comments

Comments
 (0)