Skip to content

Commit

Permalink
perf(document): remove some more unnecessary cloning in $toObject() re:
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed May 4, 2024
1 parent e910cb7 commit f087e8f
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 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 @@ -3865,27 +3865,25 @@ Document.prototype.$toObject = function(options, 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

0 comments on commit f087e8f

Please sign in to comment.