Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(document): avoid cloning options using spread operator for perf reasons #14565

Merged
merged 5 commits into from
May 6, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -3780,7 +3780,7 @@ Document.prototype.$__handleReject = function handleReject(err) {
*/

Document.prototype.$toObject = function(options, json) {
let defaultOptions = {
const defaultOptions = {
transform: true,
flattenDecimals: true
};
Expand All @@ -3793,7 +3793,7 @@ Document.prototype.$toObject = function(options, json) {
const schemaOptions = this.$__schema && this.$__schema.options || {};
// merge base default options with Schema's set default options if available.
// `clone` is necessary here because `utils.options` directly modifies the second input.
defaultOptions = { ...defaultOptions, ...baseOptions, ...schemaOptions[path] };
Object.assign(defaultOptions, baseOptions, schemaOptions[path]);

// If options do not exist or is not an object, set it to empty object
options = utils.isPOJO(options) ? { ...options } : {};
Expand Down Expand Up @@ -3839,13 +3839,6 @@ Document.prototype.$toObject = function(options, json) {
_seen: (options && options._seen) || new Map()
});

if (utils.hasUserDefinedProperty(options, 'getters')) {
cloneOptions.getters = options.getters;
}
if (utils.hasUserDefinedProperty(options, 'virtuals')) {
cloneOptions.virtuals = options.virtuals;
}

const depopulate = options.depopulate ||
(options._parentOptions && options._parentOptions.depopulate || false);
// _isNested will only be true if this is not the top level document, we
Expand All @@ -3855,33 +3848,35 @@ Document.prototype.$toObject = function(options, json) {
}

// merge default options with input options.
options = { ...defaultOptions, ...options };
for (const key of Object.keys(defaultOptions)) {
if (options[key] == null) {
options[key] = defaultOptions[key];
}
}
options._isNested = true;
options.json = json;
options.minimize = _minimize;

cloneOptions._parentOptions = options;
cloneOptions._skipSingleNestedGetters = false;

const gettersOptions = Object.assign({}, cloneOptions);
gettersOptions._skipSingleNestedGetters = true;

cloneOptions._skipSingleNestedGetters = false;
// remember the root transform function
// to save it from being overwritten by sub-transform functions
const originalTransform = options.transform;

let ret = clone(this._doc, cloneOptions) || {};

cloneOptions._skipSingleNestedGetters = true;
if (options.getters) {
applyGetters(this, ret, gettersOptions);
applyGetters(this, ret, cloneOptions);

if (options.minimize) {
ret = minimize(ret) || {};
}
}

if (options.virtuals || (options.getters && options.virtuals !== false)) {
applyVirtuals(this, ret, gettersOptions, options);
applyVirtuals(this, ret, cloneOptions, options);
}

if (options.versionKey === false && this.$__schema.options.versionKey) {
Expand Down
Loading