From 422dff428f751193b05bb673f6e3d609a376f7a4 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sat, 8 Jul 2023 19:39:41 -0400 Subject: [PATCH] perf: avoid adding all doc array subpaths when 1 path is modified --- lib/document.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/document.js b/lib/document.js index a9ee65dffdd..0918d26fbff 100644 --- a/lib/document.js +++ b/lib/document.js @@ -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... @@ -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; }