Automattic / Automattic/mongoose

Performance: avoid cloning when saving document if `minimize: false`

Open
#15,694 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

### Prerequisites

- [x] I have written a descriptive issue title
- [x] I have searched existing issues to ensure the issue has not already been raised

### Issue

In `$__handleSave()` we have the following:

```javascript
if (this.$isNew) {
// send entire doc
const obj = this.toObject(saveToObjectOptions);
```

However, if `minimize: false`, we don't need to run `toObject()`, we can just do `const obj = this._doc` because the MongoDB driver will clone and apply `toBSON()` anyway. This should significantly help performance.

Re: #8024, let's implement this functionality first and make sure it works as intended before considering making `minimize` default to false.

Contributor guide

Open the contributing guide

Research direction

Start at $__handleSave(), inspect the $isNew branch and the existing toObject(saveToObjectOptions) path. Exercise a new document with minimize: false, compare the saved behavior, and confirm the MongoDB driver's cloning and toBSON handling preserve the document without regressions.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb
Domain
database
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.