Skip to content

Commit a1b7bb5

Browse files
committed
perf(schema): make some micro optimizations for casting subdocument paths
Re: #13191
1 parent 7a90868 commit a1b7bb5

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

lib/schema/SubdocumentPath.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,22 @@ SubdocumentPath.prototype.cast = function(val, doc, init, priorVal, options) {
160160
let subdoc;
161161

162162
// Only pull relevant selected paths and pull out the base path
163-
const parentSelected = doc && doc.$__ && doc.$__.selected || {};
163+
const parentSelected = doc && doc.$__ && doc.$__.selected;
164164
const path = this.path;
165-
const selected = Object.keys(parentSelected).reduce((obj, key) => {
165+
const selected = parentSelected == null ? null : Object.keys(parentSelected).reduce((obj, key) => {
166166
if (key.startsWith(path + '.')) {
167167
obj = obj || {};
168168
obj[key.substring(path.length + 1)] = parentSelected[key];
169169
}
170170
return obj;
171171
}, null);
172-
options = Object.assign({}, options, { priorDoc: priorVal });
173172
if (init) {
174173
subdoc = new Constructor(void 0, selected, doc, false, { defaults: false });
175174
delete subdoc.$__.defaults;
176175
subdoc.$init(val);
177176
applyDefaults(subdoc, selected);
178177
} else {
178+
options = Object.assign({}, options, { priorDoc: priorVal });
179179
if (Object.keys(val).length === 0) {
180180
return new Constructor({}, selected, doc, undefined, options);
181181
}

0 commit comments

Comments
 (0)