Skip to content

Commit

Permalink
perf: remove unused cached ids and avoid unnecessarily setting a coup…
Browse files Browse the repository at this point in the history
…le of document properties

Re: #11541
  • Loading branch information
vkarpov15 committed Apr 12, 2022
1 parent caf821d commit 63af194
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 30 deletions.
8 changes: 2 additions & 6 deletions lib/document.js
Expand Up @@ -94,7 +94,7 @@ function Document(obj, fields, skipId, options) {
this.$__ = new InternalCache();
this.$isNew = 'isNew' in options ? options.isNew : true;

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

Expand All @@ -119,7 +119,7 @@ function Document(obj, fields, skipId, options) {
fields = undefined;
} else {
this.$__.strictMode = schema.options.strict;
if (fields !== undefined) {
if (fields != null) {
this.$__.selected = fields;
}
}
Expand Down Expand Up @@ -177,8 +177,6 @@ function Document(obj, fields, skipId, options) {
$__applyDefaults(this, fields, exclude, hasIncludedChildren, false, options.skipDefaults);
}

this.$__._id = this._id;

if (!this.$__.strictMode && obj) {
const _this = this;
const keys = Object.keys(this._doc);
Expand Down Expand Up @@ -743,8 +741,6 @@ Document.prototype.$__init = function(doc, opts) {
this.$emit('init', this);
this.constructor.emit('init', this);

this.$__._id = this._id;

const hasIncludedChildren = this.$__.exclude === false && this.$__.fields ?
$__hasIncludedChildren(this.$__.fields) :
null;
Expand Down
3 changes: 2 additions & 1 deletion lib/schema/SubdocumentPath.js
Expand Up @@ -167,10 +167,11 @@ SubdocumentPath.prototype.cast = function(val, doc, init, priorVal, options) {
const path = this.path;
const selected = Object.keys(parentSelected).reduce((obj, key) => {
if (key.startsWith(path + '.')) {
obj = obj || {};
obj[key.substring(path.length + 1)] = parentSelected[key];
}
return obj;
}, {});
}, null);
options = Object.assign({}, options, { priorDoc: priorVal });
if (init) {
subdoc = new Constructor(void 0, selected, doc);
Expand Down
3 changes: 0 additions & 3 deletions lib/schema/objectid.js
Expand Up @@ -282,11 +282,8 @@ function resetId(v) {
if (this instanceof Document) {
if (v === void 0) {
const _v = new oid();
this.$__._id = _v;
return _v;
}

this.$__._id = v;
}

return v;
Expand Down
1 change: 0 additions & 1 deletion lib/schematype.js
Expand Up @@ -1128,7 +1128,6 @@ SchemaType.prototype.getDefault = function(scope, init) {
ret = this.defaultValue;
}


if (ret !== null && ret !== undefined) {
if (typeof ret === 'object' && (!this.options || !this.options.shared)) {
ret = utils.clone(ret);
Expand Down
19 changes: 0 additions & 19 deletions test/types.document.test.js
Expand Up @@ -105,25 +105,6 @@ describe('types.document', function() {
assert.equal(b.work, 'good flam');
});

it('cached _ids', function() {
const Movie = db.model('Movie', MovieSchema);
const m = new Movie();

assert.equal(m.id, m.$__._id);
const old = m.id;
m._id = new mongoose.Types.ObjectId();
assert.equal(m.id, m.$__._id);
assert.strictEqual(true, old !== m.$__._id);

const m2 = new Movie();
delete m2._doc._id;
m2.init({ _id: new mongoose.Types.ObjectId() });
assert.equal(m2.id, m2.$__._id);
assert.strictEqual(true, m.$__._id !== m2.$__._id);
assert.strictEqual(true, m.id !== m2.id);
assert.strictEqual(true, m.$__._id !== m2.$__._id);
});

it('Subdocument#remove (gh-531)', async function() {
const Movie = db.model('Movie', MovieSchema);

Expand Down

0 comments on commit 63af194

Please sign in to comment.