Skip to content

Commit 635c795

Browse files
committedFeb 26, 2024·
perf(document+schema): small optimizations to make init() faster
Re: #14113
1 parent ac9ea01 commit 635c795

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed
 

‎lib/document.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ function init(self, obj, doc, opts, prefix) {
741741
if (i === '__proto__' || i === 'constructor') {
742742
return;
743743
}
744-
path = prefix + i;
744+
path = prefix ? prefix + i : i;
745745
schemaType = docSchema.path(path);
746746

747747
// Should still work if not a model-level discriminator, but should not be
@@ -751,38 +751,39 @@ function init(self, obj, doc, opts, prefix) {
751751
return;
752752
}
753753

754-
if (!schemaType && utils.isPOJO(obj[i])) {
754+
const value = obj[i];
755+
if (!schemaType && utils.isPOJO(value)) {
755756
// assume nested object
756757
if (!doc[i]) {
757758
doc[i] = {};
758759
if (!strict && !(i in docSchema.tree) && !(i in docSchema.methods) && !(i in docSchema.virtuals)) {
759760
self[i] = doc[i];
760761
}
761762
}
762-
init(self, obj[i], doc[i], opts, path + '.');
763+
init(self, value, doc[i], opts, path + '.');
763764
} else if (!schemaType) {
764-
doc[i] = obj[i];
765+
doc[i] = value;
765766
if (!strict && !prefix) {
766-
self[i] = obj[i];
767+
self[i] = value;
767768
}
768769
} else {
769770
// Retain order when overwriting defaults
770-
if (doc.hasOwnProperty(i) && obj[i] !== void 0) {
771+
if (doc.hasOwnProperty(i) && value !== void 0) {
771772
delete doc[i];
772773
}
773-
if (obj[i] === null) {
774+
if (value === null) {
774775
doc[i] = schemaType._castNullish(null);
775-
} else if (obj[i] !== undefined) {
776-
const wasPopulated = obj[i].$__ == null ? null : obj[i].$__.wasPopulated;
776+
} else if (value !== undefined) {
777+
const wasPopulated = value.$__ == null ? null : value.$__.wasPopulated;
777778

778779
if (schemaType && !wasPopulated) {
779780
try {
780781
if (opts && opts.setters) {
781782
// Call applySetters with `init = false` because otherwise setters are a noop
782783
const overrideInit = false;
783-
doc[i] = schemaType.applySetters(obj[i], self, overrideInit);
784+
doc[i] = schemaType.applySetters(value, self, overrideInit);
784785
} else {
785-
doc[i] = schemaType.cast(obj[i], self, true);
786+
doc[i] = schemaType.cast(value, self, true);
786787
}
787788
} catch (e) {
788789
self.invalidate(e.path, new ValidatorError({
@@ -794,7 +795,7 @@ function init(self, obj, doc, opts, prefix) {
794795
}));
795796
}
796797
} else {
797-
doc[i] = obj[i];
798+
doc[i] = value;
798799
}
799800
}
800801
// mark as hydrated

‎lib/schema.js

+3
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,9 @@ reserved.collection = 1;
955955

956956
Schema.prototype.path = function(path, obj) {
957957
if (obj === undefined) {
958+
if (this.paths[path] != null) {
959+
return this.paths[path];
960+
}
958961
// Convert to '.$' to check subpaths re: gh-6405
959962
const cleanPath = _pathToPositionalSyntax(path);
960963
let schematype = _getPath(this, path, cleanPath);

0 commit comments

Comments
 (0)
Please sign in to comment.