Skip to content

Commit

Permalink
perf: some more small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jul 15, 2023
1 parent 69405b2 commit dcc4c9a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
28 changes: 12 additions & 16 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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;
}
Expand All @@ -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);
}
}

Expand Down
7 changes: 4 additions & 3 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand All @@ -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';
}
Expand Down

0 comments on commit dcc4c9a

Please sign in to comment.