-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
perf(query): skip recursive casting for simple equality filters #15754
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(query): skip recursive casting for simple equality filters #15754
Conversation
|
@vkarpov15 can you approve this please. |
| } | ||
| function _isSimpleEqualityFilter(filter, model) { | ||
| if (!filter || typeof filter !== 'object' || Array.isArray(filter)) return false; | ||
| const keys = Object.keys(filter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't that expensive to create an array instead of a for loop ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@billouboq
This approach is crucial for correctly validating the filter structure because it offers two key safety guarantees:
It guarantees iteration only over the object's own enumerable properties.
It automatically excludes inherited properties (which a standard for...in loop would include without an extra hasOwnProperty check).
vkarpov15
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR skips all casting if you have only string/number/boolean properties in your filter, which is incorrect behavior.
Can you please put together a benchmark script which demonstrates a case where this change would make for a significant performance improvement?
|
|
||
| const t = typeof v; | ||
| if (t === 'string' || t === 'number' || t === 'boolean' || v instanceof RegExp) { | ||
| const schemaType = model && model.schema ? model.schema.path(k) : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't check if the t lines up with schemaType. If you pass in email: 42 this will skip casting number 42 to a string.
| const t = typeof v; | ||
| if (t === 'string' || t === 'number' || t === 'boolean' || v instanceof RegExp) { | ||
| const schemaType = model && model.schema ? model.schema.path(k) : null; | ||
| if (schemaType && schemaType.instance && /ObjectId/i.test(schemaType.instance)) return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also wouldn't handle if schemaType is an object, array, or map.
|
I'm going to close this PR since I don't think it is salvageable. |
Summary
This PR introduces a conservative performance optimization in
Query.prototype._castConditions.For simple equality filters (e.g.
{ name: "Alice" },{ age: 20 }), Mongoose currently performs the full recursive casting pipeline, which adds noticeable overhead compared to the native MongoDB driver.This PR adds a fast-path that skips recursive casting when the filter meets all of the following conditions:
$...)string,number,boolean),RegExp, ornullIf all conditions are satisfied,
_castConditionsreturns early and avoids the expensive casting process.Motivation
Mongoose users have reported slower query performance compared to the native driver, especially for frequently-used simple lookups such as: