Skip to content

Commit c51fdd8

Browse files
committed
perf(document): avoid copying all properties into cloneOptions, just the necessary ones
Re: #14394
1 parent abc87b2 commit c51fdd8

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
lines changed

lib/document.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3830,14 +3830,18 @@ Document.prototype.$toObject = function(options, json) {
38303830
// `clone()` will recursively call `$toObject()` on embedded docs, so we
38313831
// need the original options the user passed in, plus `_isNested` and
38323832
// `_parentOptions` for checking whether we need to depopulate.
3833-
const cloneOptions = Object.assign({}, options, {
3833+
const cloneOptions = {
38343834
_isNested: true,
38353835
json: json,
38363836
minimize: _minimize,
38373837
flattenMaps: flattenMaps,
38383838
flattenObjectIds: flattenObjectIds,
3839-
_seen: (options && options._seen) || new Map()
3840-
});
3839+
_seen: (options && options._seen) || new Map(),
3840+
_calledWithOptions: options._calledWithOptions,
3841+
virtuals: options.virtuals,
3842+
getters: options.getters,
3843+
depopulate: options.depopulate
3844+
};
38413845

38423846
const depopulate = options.depopulate ||
38433847
(options._parentOptions && options._parentOptions.depopulate || false);

test/document.test.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -722,32 +722,27 @@ describe('document', function() {
722722
lastName: String,
723723
password: String
724724
});
725-
726725
userSchema.virtual('fullName').get(function() {
727726
return this.firstName + ' ' + this.lastName;
728727
});
729-
730728
userSchema.set('toObject', { virtuals: false });
731729

732730
const postSchema = new Schema({
733731
owner: { type: Schema.Types.ObjectId, ref: 'User' },
734732
content: String
735733
});
736-
737734
postSchema.virtual('capContent').get(function() {
738735
return this.content.toUpperCase();
739736
});
740-
741737
postSchema.set('toObject', { virtuals: true });
738+
742739
const User = db.model('User', userSchema);
743740
const Post = db.model('BlogPost', postSchema);
744741

745742
const user = new User({ firstName: 'Joe', lastName: 'Smith', password: 'password' });
746-
747743
const savedUser = await user.save();
748744

749745
const post = await Post.create({ owner: savedUser._id, content: 'lorem ipsum' });
750-
751746
const newPost = await Post.findById(post._id).populate('owner').exec();
752747

753748
const obj = newPost.toObject();

test/model.populate.test.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2976,33 +2976,27 @@ describe('model: populate:', function() {
29762976
return ret;
29772977
}
29782978
});
2979-
29802979
const Team = db.model('Test', teamSchema);
29812980

29822981
const userSchema = new Schema({
29832982
username: String
29842983
});
2985-
29862984
userSchema.set('toJSON', {
29872985
transform: function(doc, ret) {
29882986
return ret;
29892987
}
29902988
});
2991-
29922989
const User = db.model('User', userSchema);
29932990

29942991
const user = new User({ username: 'Test' });
2995-
29962992
await user.save();
29972993

29982994
const team = new Team({ members: [{ user: user }] });
2999-
30002995
await team.save();
3001-
30022996
await team.populate('members.user');
30032997

2998+
assert.equal(calls, 0);
30042999
team.toJSON();
3005-
30063000
assert.equal(calls, 1);
30073001
});
30083002

0 commit comments

Comments
 (0)