Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(schema): clear childSchemas when overwriting existing path to avoid performance degradations #15256

Merged
merged 1 commit into from
Feb 14, 2025

Conversation

vkarpov15
Copy link
Collaborator

Fix #15253

Summary

Currently, overwriting an existing recursive path using Schema.prototype.path() leads to an exponential performance blowup because we're adding a separate recursive path to childSchemas on every path() call. For example, in the test case added, the document has 1092 subdocuments, but with 2 duplicate childSchemas entry Mongoose thinks there's over 56k subdocuments. With 3, Mongoose thinks there's about 560k subdocuments. I haven't been able to figure out a more complete explanation of the exact mathematical model of this growth, but suffice to say cleaning up duplicate childSchemas fixes the issue.

Examples

@vkarpov15 vkarpov15 added this to the 8.10.1 milestone Feb 13, 2025
@vkarpov15 vkarpov15 merged commit d48bc5f into master Feb 14, 2025
75 checks passed
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-15253 branch February 14, 2025 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Changing a schema path using schema.path significantly affects performance
2 participants