Skip to content

Commit

Permalink
perf(document+statemachine): remove some unnecessary duplicated fields
Browse files Browse the repository at this point in the history
Re: #11541
  • Loading branch information
vkarpov15 committed Jun 26, 2022
1 parent 1ccbed6 commit 989525c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
13 changes: 5 additions & 8 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ function Document(obj, fields, skipId, options) {
fields = undefined;
} else {
this.$__.strictMode = schema.options.strict;
if (fields != null) {
this.$__.selected = fields;
}
}

const requiredPaths = schema.requiredPaths(true);
Expand All @@ -134,9 +131,9 @@ function Document(obj, fields, skipId, options) {

// determine if this doc is a result of a query with
// excluded fields
if (utils.isPOJO(fields)) {
if (utils.isPOJO(fields) && Object.keys(fields).length > 0) {
exclude = isExclusive(fields);
this.$__.fields = fields;
this.$__.selected = fields;
this.$__.exclude = exclude;
}

Expand Down Expand Up @@ -745,10 +742,10 @@ Document.prototype.$__init = function(doc, opts) {
this.$emit('init', this);
this.constructor.emit('init', this);

const hasIncludedChildren = this.$__.exclude === false && this.$__.fields ?
$__hasIncludedChildren(this.$__.fields) :
const hasIncludedChildren = this.$__.exclude === false && this.$__.selected ?
$__hasIncludedChildren(this.$__.selected) :
null;
$__applyDefaults(this, this.$__.fields, this.$__.exclude, hasIncludedChildren, false, this.$__.skipDefaults);
$__applyDefaults(this, this.$__.selected, this.$__.exclude, hasIncludedChildren, false, this.$__.skipDefaults);

return this;
};
Expand Down
3 changes: 2 additions & 1 deletion lib/statemachine.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ StateMachine.ctor = function() {
StateMachine.apply(this, arguments);
this.paths = {};
this.states = {};
this.stateNames = states;
};

ctor.prototype = new StateMachine();

ctor.prototype.stateNames = states;

states.forEach(function(state) {
// Changes the `path`'s state to `state`.
ctor.prototype[state] = function(path) {
Expand Down
6 changes: 3 additions & 3 deletions test/docs/lean.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ describe('Lean Tutorial', function() {
// To enable the `lean` option for a query, use the `lean()` function.
const leanDoc = await MyModel.findOne().lean();

v8Serialize(normalDoc).length; // approximately 300
v8Serialize(leanDoc).length; // 32, more than 10x smaller!
v8Serialize(normalDoc).length; // approximately 180
v8Serialize(leanDoc).length; // 32, about 5x smaller!

// In case you were wondering, the JSON form of a Mongoose doc is the same
// as the POJO. This additional memory only affects how much memory your
// Node.js process uses, not how much data is sent over the network.
JSON.stringify(normalDoc).length === JSON.stringify(leanDoc).length; // true
// acquit:ignore:start
assert.ok(v8Serialize(normalDoc).length >= 300 && v8Serialize(normalDoc).length <= 800, v8Serialize(normalDoc).length);
assert.ok(v8Serialize(normalDoc).length >= 150 && v8Serialize(normalDoc).length <= 200, v8Serialize(normalDoc).length);
assert.equal(v8Serialize(leanDoc).length, 32);
assert.equal(JSON.stringify(normalDoc).length, JSON.stringify(leanDoc).length);
// acquit:ignore:end
Expand Down

0 comments on commit 989525c

Please sign in to comment.