Skip to content

Commit 75c6f88

Browse files
committed
perf: memoize default toObject options on a per-schema basis re: #14394
1 parent a40706b commit 75c6f88

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

lib/document.js

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const clone = require('./helpers/clone');
2222
const compile = require('./helpers/document/compile').compile;
2323
const defineKey = require('./helpers/document/compile').defineKey;
2424
const flatten = require('./helpers/common').flatten;
25-
const get = require('./helpers/get');
2625
const getEmbeddedDiscriminatorPath = require('./helpers/document/getEmbeddedDiscriminatorPath');
2726
const getKeysInSchemaOrder = require('./helpers/schema/getKeysInSchemaOrder');
2827
const getSubdocumentStrictValue = require('./helpers/schema/getSubdocumentStrictValue');
@@ -3798,15 +3797,7 @@ Document.prototype.$__handleReject = function handleReject(err) {
37983797
*/
37993798

38003799
Document.prototype.$toObject = function(options, json) {
3801-
const path = json ? 'toJSON' : 'toObject';
3802-
const baseOptions = this.constructor &&
3803-
this.constructor.base &&
3804-
this.constructor.base.options &&
3805-
get(this.constructor.base.options, path) || {};
3806-
const schemaOptions = this.$__schema && this.$__schema.options || {};
3807-
// merge base default options with Schema's set default options if available.
3808-
// `clone` is necessary here because `utils.options` directly modifies the second input.
3809-
const defaultOptions = Object.assign({}, baseOptions, schemaOptions[path]);
3800+
const defaultOptions = this.$__schema._getDefaultToObjectOptions();
38103801

38113802
// If options do not exist or is not an object, set it to empty object
38123803
options = utils.isPOJO(options) ? { ...options } : {};
@@ -3815,10 +3806,8 @@ Document.prototype.$toObject = function(options, json) {
38153806
let _minimize;
38163807
if (options._calledWithOptions.minimize != null) {
38173808
_minimize = options.minimize;
3818-
} else if (defaultOptions.minimize != null) {
3819-
_minimize = defaultOptions.minimize;
38203809
} else {
3821-
_minimize = schemaOptions.minimize;
3810+
_minimize = defaultOptions.minimize;
38223811
}
38233812

38243813
options.minimize = _minimize;
@@ -3842,14 +3831,8 @@ Document.prototype.$toObject = function(options, json) {
38423831
}
38433832
options._isNested = true;
38443833
options.json = json;
3845-
options.minimize = _minimize;
3846-
38473834
options._parentOptions = options;
3848-
38493835
options._skipSingleNestedGetters = false;
3850-
// remember the root transform function
3851-
// to save it from being overwritten by sub-transform functions
3852-
// const originalTransform = options.transform;
38533836

38543837
let ret = clone(this._doc, options) || {};
38553838

lib/schema.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ function Schema(obj, options) {
128128
// For internal debugging. Do not use this to try to save a schema in MDB.
129129
this.$id = ++id;
130130
this.mapPaths = [];
131+
this._defaultToObjectOptions = null;
131132

132133
this.s = {
133134
hooks: new Kareem()
@@ -2597,6 +2598,33 @@ Schema.prototype.loadClass = function(model, virtualsOnly) {
25972598
return this;
25982599
};
25992600

2601+
/**
2602+
* Returns default `toObject` / `toJSON` options for this schema,
2603+
* combining the associated
2604+
*
2605+
* @param {Boolean} json
2606+
* @returns object
2607+
*/
2608+
2609+
Schema.prototype._getDefaultToObjectOptions = function _getDefaultToObjectOptions(json) {
2610+
const path = json ? 'toJSON' : 'toObject';
2611+
2612+
if (this._defaultToObjectOptions && this._defaultToObjectOptions[path]) {
2613+
return this._defaultToObjectOptions[path];
2614+
}
2615+
2616+
const baseOptions = this.base && this.base.options && this.base.options[path];
2617+
const schemaOptions = this.options && this.options[path];
2618+
const defaultOptions = Object.assign(
2619+
{},
2620+
baseOptions,
2621+
schemaOptions
2622+
);
2623+
this._defaultToObjectOptions = this._defaultToObjectOptions || {};
2624+
this._defaultToObjectOptions[path] = defaultOptions;
2625+
return defaultOptions;
2626+
};
2627+
26002628
/*!
26012629
* ignore
26022630
*/

0 commit comments

Comments
 (0)