Skip to content

Commit

Permalink
perf(document): avoid running validation on every array element if th…
Browse files Browse the repository at this point in the history
…ere's no validators to run

Re: #11380
  • Loading branch information
vkarpov15 committed Mar 12, 2022
1 parent 5bdd836 commit 94d77c2
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,16 @@ function _getPathsToValidate(doc) {
continue;
}

// gh-11380: optimization. If the array isn't a document array and there's no validators
// on the array type, there's no need to run validation on the individual array elements.
if (_pathType &&
_pathType.$isMongooseArray &&
!_pathType.$isMongooseDocumentArray && // Skip document arrays...
!_pathType.$embeddedSchemaType.$isMongooseArray && // and arrays of arrays
_pathType.$embeddedSchemaType.validators.length === 0) {
continue;
}

const val = doc.$__getValue(path);
_pushNestedArrayPaths(val, paths, path);
}
Expand Down

0 comments on commit 94d77c2

Please sign in to comment.