Skip to content

Commit

Permalink
perf(document+internal): put default value for $isNew and `strictMo…
Browse files Browse the repository at this point in the history
…de` on prototype to avoid unnecessary memory usage

Re: #11541
  • Loading branch information
vkarpov15 committed Jun 26, 2022
1 parent 989525c commit 9e05baf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
19 changes: 16 additions & 3 deletions lib/document.js
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion lib/internal.js
Expand Up @@ -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;
Expand Down

0 comments on commit 9e05baf

Please sign in to comment.