Skip to content

Commit

Permalink
perf: avoid adding all doc array subpaths when 1 path is modified
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jul 8, 2023
1 parent eb9a4f7 commit 422dff4
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -2682,15 +2682,9 @@ function _getPathsToValidate(doc, pathsToValidate, pathsToSkip) {
}
}

// If underneath a document array, may need to re-validate the parent
// array re: gh-6818
if (_pathType.$parentSchemaDocArray && typeof _pathType.$parentSchemaDocArray.path === 'string') {
paths.add(_pathType.$parentSchemaDocArray.path);
}

// Optimization: if primitive path with no validators, or array of primitives
// with no validators, skip validating this path entirely.
if (!_pathType.caster && _pathType.validators.length === 0) {
if (!_pathType.caster && _pathType.validators.length === 0 && !_pathType.$parentSchemaDocArray) {
paths.delete(path);
} else if (_pathType.$isMongooseArray &&
!_pathType.$isMongooseDocumentArray && // Skip document arrays...
Expand Down Expand Up @@ -2777,7 +2771,19 @@ function _getPathsToValidate(doc, pathsToValidate, pathsToSkip) {

for (const path of paths) {
const _pathType = doc.$__schema.path(path);
if (!_pathType || !_pathType.$isSchemaMap) {

if (!_pathType) {
continue;
}

// If underneath a document array, may need to re-validate the parent
// array re: gh-6818. Do this _after_ adding subpaths, because
// we don't want to add every array subpath.
if (_pathType.$parentSchemaDocArray && typeof _pathType.$parentSchemaDocArray.path === 'string') {
paths.add(_pathType.$parentSchemaDocArray.path);
}

if (!_pathType.$isSchemaMap) {
continue;
}

Expand Down

0 comments on commit 422dff4

Please sign in to comment.