From dcc4c9a5ce0a1f2e6efceb9742acde0e58027f8d Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 14 Jul 2023 21:24:33 -0400 Subject: [PATCH] perf: some more small optimizations --- lib/document.js | 28 ++++++++++++---------------- lib/schema.js | 7 ++++--- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/lib/document.js b/lib/document.js index c4b8ab647a4..4222ea2757f 100644 --- a/lib/document.js +++ b/lib/document.js @@ -1112,9 +1112,11 @@ Document.prototype.$set = function $set(path, val, type, options) { return this; } + options = Object.assign({}, options, { _skipMinimizeTopLevel: false }); + for (let i = 0; i < len; ++i) { key = keys[i]; - const pathName = prefix + key; + const pathName = prefix ? prefix + key : key; pathtype = this.$__schema.pathType(pathName); const valForKey = path[key]; @@ -1126,20 +1128,15 @@ Document.prototype.$set = function $set(path, val, type, options) { pathtype === 'nested' && this._doc[key] != null) { delete this._doc[key]; - // Make sure we set `{}` back even if we minimize re: gh-8565 - options = Object.assign({}, options, { _skipMinimizeTopLevel: true }); - } else { - // Make sure we set `{_skipMinimizeTopLevel: false}` if don't have overwrite: gh-10441 - options = Object.assign({}, options, { _skipMinimizeTopLevel: false }); } if (utils.isNonBuiltinObject(valForKey) && pathtype === 'nested') { - this.$set(pathName, path[key], constructing, Object.assign({}, options, { _skipMarkModified: true })); - $applyDefaultsToNested(this.$get(oathName), pathName, this); + this.$set(pathName, valForKey, constructing, Object.assign({}, options, { _skipMarkModified: true })); + $applyDefaultsToNested(this.$get(pathName), pathName, this); continue; } else if (strict) { // Don't overwrite defaults with undefined keys (gh-3981) (gh-9039) - if (constructing && path[key] === void 0 && + if (constructing && valForKey === void 0 && this.$get(pathName) !== void 0) { continue; } @@ -1149,20 +1146,19 @@ Document.prototype.$set = function $set(path, val, type, options) { } if (pathtype === 'real' || pathtype === 'virtual') { - const p = path[key]; - this.$set(pathName, p, constructing, options); - } else if (pathtype === 'nested' && path[key] instanceof Document) { + this.$set(pathName, valForKey, constructing, options); + } else if (pathtype === 'nested' && valForKey instanceof Document) { this.$set(pathName, - path[key].toObject({ transform: false }), constructing, options); + valForKey.toObject({ transform: false }), constructing, options); } else if (strict === 'throw') { if (pathtype === 'nested') { - throw new ObjectExpectedError(key, path[key]); + throw new ObjectExpectedError(key, valForKey); } else { throw new StrictModeError(key); } } - } else if (path[key] !== void 0) { - this.$set(pathName, path[key], constructing, options); + } else if (valForKey !== void 0) { + this.$set(pathName, valForKey, constructing, options); } } diff --git a/lib/schema.js b/lib/schema.js index fd43b15010f..5191b60b3ee 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -1530,9 +1530,6 @@ Schema.prototype.indexedPaths = function indexedPaths() { */ Schema.prototype.pathType = function(path) { - // Convert to '.$' to check subpaths re: gh-6405 - const cleanPath = _pathToPositionalSyntax(path); - if (this.paths.hasOwnProperty(path)) { return 'real'; } @@ -1542,6 +1539,10 @@ Schema.prototype.pathType = function(path) { if (this.nested.hasOwnProperty(path)) { return 'nested'; } + + // Convert to '.$' to check subpaths re: gh-6405 + const cleanPath = _pathToPositionalSyntax(path); + if (this.subpaths.hasOwnProperty(cleanPath) || this.subpaths.hasOwnProperty(path)) { return 'real'; }