Skip to content

Commit 670b445

Browse files
committed
Revert "perf: make a couple of more minor performance optimizations re: #11541"
This reverts commit c3b223f.
1 parent 213607f commit 670b445

File tree

5 files changed

+14
-15
lines changed

5 files changed

+14
-15
lines changed

lib/document.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ function Document(obj, fields, skipId, options) {
7979
options = skipId;
8080
skipId = options.skipId;
8181
}
82+
options = Object.assign({}, options);
8283

8384
// Support `browserDocument.js` syntax
8485
if (this.$__schema == null) {
@@ -92,9 +93,9 @@ function Document(obj, fields, skipId, options) {
9293
}
9394

9495
this.$__ = new InternalCache();
95-
this.$isNew = options && options.isNew != null ? options.isNew : true;
96+
this.$isNew = 'isNew' in options ? options.isNew : true;
9697

97-
if (options && options.priorDoc != null) {
98+
if (options.priorDoc != null) {
9899
this.$__.priorDoc = options.priorDoc;
99100
}
100101

@@ -107,20 +108,18 @@ function Document(obj, fields, skipId, options) {
107108
}
108109

109110
let defaults = true;
110-
if (options && options.defaults !== undefined) {
111+
if (options.defaults !== undefined) {
111112
this.$__.defaults = options.defaults;
112113
defaults = options.defaults;
113114
}
114115

115116
const schema = this.$__schema;
116117

117-
let strict;
118118
if (typeof fields === 'boolean' || fields === 'throw') {
119119
this.$__.strictMode = fields;
120-
strict = fields;
121120
fields = undefined;
122121
} else {
123-
strict = schema.options.strict;
122+
this.$__.strictMode = schema.options.strict;
124123
if (fields != null) {
125124
this.$__.selected = fields;
126125
}
@@ -170,15 +169,15 @@ function Document(obj, fields, skipId, options) {
170169
// Function defaults get applied **after** setting initial values so they
171170
// see the full doc rather than an empty one, unless they opt out.
172171
// Re: gh-3781, gh-6155
173-
if (options && options.willInit && defaults) {
172+
if (options.willInit && defaults) {
174173
if (options.skipDefaults) {
175174
this.$__.skipDefaults = options.skipDefaults;
176175
}
177176
} else if (defaults) {
178-
$__applyDefaults(this, fields, exclude, hasIncludedChildren, false, options && options.skipDefaults);
177+
$__applyDefaults(this, fields, exclude, hasIncludedChildren, false, options.skipDefaults);
179178
}
180179

181-
if (!strict && obj) {
180+
if (!this.$__.strictMode && obj) {
182181
const _this = this;
183182
const keys = Object.keys(this._doc);
184183

lib/helpers/schematype/handleImmutable.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ function createImmutableSetter(path, immutable) {
4040
const _value = this.$__.priorDoc != null ?
4141
this.$__.priorDoc.$__getValue(path) :
4242
this.$__getValue(path);
43-
const strict = this.$__.strictMode == null ? this.$__schema.options.strict : this.$__.strictMode;
44-
if (strict === 'throw' && v !== _value) {
43+
if (this.$__.strictMode === 'throw' && v !== _value) {
4544
throw new StrictModeError(path, 'Path `' + path + '` is immutable ' +
4645
'and strict mode is set to throw.', true);
4746
}

lib/schema/SubdocumentPath.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ SubdocumentPath.prototype.cast = function(val, doc, init, priorVal, options) {
167167
}
168168
return obj;
169169
}, null);
170-
options = priorVal != null ? Object.assign({}, options, { priorDoc: priorVal }) : options;
170+
options = Object.assign({}, options, { priorDoc: priorVal });
171171
if (init) {
172172
subdoc = new Constructor(void 0, selected, doc);
173173
subdoc.$init(val);

lib/schema/documentarray.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,8 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
391391
let selected;
392392
let subdoc;
393393

394+
options = options || {};
395+
394396
if (!Array.isArray(value)) {
395397
if (!init && !DocumentArrayPath.options.castNonArrays) {
396398
throw new CastError('DocumentArray', value, this.path, null, this);
@@ -405,15 +407,15 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
405407

406408
// We need to create a new array, otherwise change tracking will
407409
// update the old doc (gh-4449)
408-
if (!options || !options.skipDocumentArrayCast || utils.isMongooseDocumentArray(value)) {
410+
if (!options.skipDocumentArrayCast || utils.isMongooseDocumentArray(value)) {
409411
value = new MongooseDocumentArray(value, this.path, doc);
410412
}
411413

412414
if (prev != null) {
413415
value[arrayAtomicsSymbol] = prev[arrayAtomicsSymbol] || {};
414416
}
415417

416-
if (options && options.arrayPathIndex != null) {
418+
if (options.arrayPathIndex != null) {
417419
value[arrayPathSymbol] = this.path + '.' + options.arrayPathIndex;
418420
}
419421

lib/schematype.js

-1
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,6 @@ SchemaType.prototype._castNullish = function _castNullish(v) {
11811181

11821182
SchemaType.prototype.applySetters = function(value, scope, init, priorVal, options) {
11831183
let v = this._applySetters(value, scope, init, priorVal, options);
1184-
11851184
if (v == null) {
11861185
return this._castNullish(v);
11871186
}

0 commit comments

Comments
 (0)