Skip to content

Commit

Permalink
perf(document): avoid copying all properties into cloneOptions, just …
Browse files Browse the repository at this point in the history
…the necessary ones

Re: #14394
  • Loading branch information
vkarpov15 committed May 6, 2024
1 parent abc87b2 commit c51fdd8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
10 changes: 7 additions & 3 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -3830,14 +3830,18 @@ Document.prototype.$toObject = function(options, json) {
// `clone()` will recursively call `$toObject()` on embedded docs, so we
// need the original options the user passed in, plus `_isNested` and
// `_parentOptions` for checking whether we need to depopulate.
const cloneOptions = Object.assign({}, options, {
const cloneOptions = {
_isNested: true,
json: json,
minimize: _minimize,
flattenMaps: flattenMaps,
flattenObjectIds: flattenObjectIds,
_seen: (options && options._seen) || new Map()
});
_seen: (options && options._seen) || new Map(),
_calledWithOptions: options._calledWithOptions,
virtuals: options.virtuals,
getters: options.getters,
depopulate: options.depopulate
};

const depopulate = options.depopulate ||
(options._parentOptions && options._parentOptions.depopulate || false);
Expand Down
7 changes: 1 addition & 6 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -722,32 +722,27 @@ describe('document', function() {
lastName: String,
password: String
});

userSchema.virtual('fullName').get(function() {
return this.firstName + ' ' + this.lastName;
});

userSchema.set('toObject', { virtuals: false });

const postSchema = new Schema({
owner: { type: Schema.Types.ObjectId, ref: 'User' },
content: String
});

postSchema.virtual('capContent').get(function() {
return this.content.toUpperCase();
});

postSchema.set('toObject', { virtuals: true });

Check failure on line 738 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed

Check failure on line 738 in test/document.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed
const User = db.model('User', userSchema);
const Post = db.model('BlogPost', postSchema);

const user = new User({ firstName: 'Joe', lastName: 'Smith', password: 'password' });

const savedUser = await user.save();

const post = await Post.create({ owner: savedUser._id, content: 'lorem ipsum' });

const newPost = await Post.findById(post._id).populate('owner').exec();

const obj = newPost.toObject();
Expand Down
8 changes: 1 addition & 7 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2976,33 +2976,27 @@ describe('model: populate:', function() {
return ret;
}
});

const Team = db.model('Test', teamSchema);

const userSchema = new Schema({
username: String
});

userSchema.set('toJSON', {
transform: function(doc, ret) {
return ret;
}
});

const User = db.model('User', userSchema);

const user = new User({ username: 'Test' });

await user.save();

const team = new Team({ members: [{ user: user }] });

await team.save();

await team.populate('members.user');

assert.equal(calls, 0);
team.toJSON();

assert.equal(calls, 1);
});

Expand Down

0 comments on commit c51fdd8

Please sign in to comment.