Skip to content

Commit

Permalink
Revert "perf: make a couple of more minor performance optimizations re:
Browse files Browse the repository at this point in the history
#11541"

This reverts commit c3b223f.
  • Loading branch information
vkarpov15 committed Jun 13, 2022
1 parent 213607f commit 670b445
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
17 changes: 8 additions & 9 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function Document(obj, fields, skipId, options) {
options = skipId;
skipId = options.skipId;
}
options = Object.assign({}, options);

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

this.$__ = new InternalCache();
this.$isNew = options && options.isNew != null ? options.isNew : true;
this.$isNew = 'isNew' in options ? options.isNew : true;

if (options && options.priorDoc != null) {
if (options.priorDoc != null) {
this.$__.priorDoc = options.priorDoc;
}

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

let defaults = true;
if (options && options.defaults !== undefined) {
if (options.defaults !== undefined) {
this.$__.defaults = options.defaults;
defaults = options.defaults;
}

const schema = this.$__schema;

let strict;
if (typeof fields === 'boolean' || fields === 'throw') {
this.$__.strictMode = fields;
strict = fields;
fields = undefined;
} else {
strict = schema.options.strict;
this.$__.strictMode = schema.options.strict;
if (fields != null) {
this.$__.selected = fields;
}
Expand Down Expand Up @@ -170,15 +169,15 @@ function Document(obj, fields, skipId, options) {
// Function defaults get applied **after** setting initial values so they
// see the full doc rather than an empty one, unless they opt out.
// Re: gh-3781, gh-6155
if (options && options.willInit && defaults) {
if (options.willInit && defaults) {
if (options.skipDefaults) {
this.$__.skipDefaults = options.skipDefaults;
}
} else if (defaults) {
$__applyDefaults(this, fields, exclude, hasIncludedChildren, false, options && options.skipDefaults);
$__applyDefaults(this, fields, exclude, hasIncludedChildren, false, options.skipDefaults);
}

if (!strict && obj) {
if (!this.$__.strictMode && obj) {
const _this = this;
const keys = Object.keys(this._doc);

Expand Down
3 changes: 1 addition & 2 deletions lib/helpers/schematype/handleImmutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ function createImmutableSetter(path, immutable) {
const _value = this.$__.priorDoc != null ?
this.$__.priorDoc.$__getValue(path) :
this.$__getValue(path);
const strict = this.$__.strictMode == null ? this.$__schema.options.strict : this.$__.strictMode;
if (strict === 'throw' && v !== _value) {
if (this.$__.strictMode === 'throw' && v !== _value) {
throw new StrictModeError(path, 'Path `' + path + '` is immutable ' +
'and strict mode is set to throw.', true);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/schema/SubdocumentPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ SubdocumentPath.prototype.cast = function(val, doc, init, priorVal, options) {
}
return obj;
}, null);
options = priorVal != null ? Object.assign({}, options, { priorDoc: priorVal }) : options;
options = Object.assign({}, options, { priorDoc: priorVal });
if (init) {
subdoc = new Constructor(void 0, selected, doc);
subdoc.$init(val);
Expand Down
6 changes: 4 additions & 2 deletions lib/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
let selected;
let subdoc;

options = options || {};

if (!Array.isArray(value)) {
if (!init && !DocumentArrayPath.options.castNonArrays) {
throw new CastError('DocumentArray', value, this.path, null, this);
Expand All @@ -405,15 +407,15 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {

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

if (prev != null) {
value[arrayAtomicsSymbol] = prev[arrayAtomicsSymbol] || {};
}

if (options && options.arrayPathIndex != null) {
if (options.arrayPathIndex != null) {
value[arrayPathSymbol] = this.path + '.' + options.arrayPathIndex;
}

Expand Down
1 change: 0 additions & 1 deletion lib/schematype.js
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,6 @@ SchemaType.prototype._castNullish = function _castNullish(v) {

SchemaType.prototype.applySetters = function(value, scope, init, priorVal, options) {
let v = this._applySetters(value, scope, init, priorVal, options);

if (v == null) {
return this._castNullish(v);
}
Expand Down

0 comments on commit 670b445

Please sign in to comment.