From 9e05bafe2f221fdf78b3e591b96c75ffddc04e5a Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sat, 25 Jun 2022 21:49:03 -0400 Subject: [PATCH] perf(document+internal): put default value for `$isNew` and `strictMode` on prototype to avoid unnecessary memory usage Re: #11541 --- lib/document.js | 19 ++++++++++++++++--- lib/internal.js | 3 ++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/document.js b/lib/document.js index b74b04988a6..d30e789a099 100644 --- a/lib/document.js +++ b/lib/document.js @@ -93,7 +93,11 @@ function Document(obj, fields, skipId, options) { } this.$__ = new InternalCache(); - this.$isNew = 'isNew' in options ? options.isNew : true; + + // Avoid setting `isNew` to `true`, because it is `true` by default + if (options.isNew != null && options.isNew !== true) { + this.$isNew = options.isNew; + } if (options.priorDoc != null) { this.$__.priorDoc = options.priorDoc; @@ -116,9 +120,11 @@ function Document(obj, fields, skipId, options) { const schema = this.$__schema; if (typeof fields === 'boolean' || fields === 'throw') { - this.$__.strictMode = fields; + if (fields !== true) { + this.$__.strictMode = fields; + } fields = undefined; - } else { + } else if (schema.options.strict !== true) { this.$__.strictMode = schema.options.strict; } @@ -207,6 +213,13 @@ Object.defineProperty(Document.prototype, 'errors', { this.$errors = value; } }); + +/*! + * ignore + */ + +Document.prototype.$isNew = true; + /*! * Document exposes the NodeJS event emitter API, so you can use * `on`, `once`, etc. diff --git a/lib/internal.js b/lib/internal.js index a31714825f9..d7fe08d5b87 100644 --- a/lib/internal.js +++ b/lib/internal.js @@ -13,8 +13,9 @@ function InternalCache() { this.activePaths = new ActiveRoster(); } +InternalCache.prototype.strictMode = true; + InternalCache.prototype.fullPath = undefined; -InternalCache.prototype.strictMode = undefined; InternalCache.prototype.selected = undefined; InternalCache.prototype.shardval = undefined; InternalCache.prototype.saveError = undefined;