-
-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2368,7 +2368,37 @@ Query.prototype._castConditions = function() { | |
|
|
||
| if (sanitizeFilterOpt) { | ||
| sanitizeFilter(this._conditions); | ||
|
|
||
| } | ||
| function _isSimpleEqualityFilter(filter, model) { | ||
| if (!filter || typeof filter !== 'object' || Array.isArray(filter)) return false; | ||
| const keys = Object.keys(filter); | ||
| if (keys.length === 0) return false; | ||
|
|
||
| for (let i = 0; i < keys.length; ++i) { | ||
| const k = keys[i]; | ||
| if (k.charAt(0) === '$') return false; | ||
|
|
||
| const v = filter[k]; | ||
| if (v === null) continue; | ||
|
|
||
| const t = typeof v; | ||
| if (t === 'string' || t === 'number' || t === 'boolean' || v instanceof RegExp) { | ||
| const schemaType = model && model.schema ? model.schema.path(k) : null; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't check if the |
||
| if (schemaType && schemaType.instance && /ObjectId/i.test(schemaType.instance)) return false; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also wouldn't handle if |
||
| continue; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| if (_isSimpleEqualityFilter(this._conditions, this.model)) { | ||
| return; | ||
| } | ||
|
|
||
|
|
||
| try { | ||
| this.cast(this.model); | ||
|
|
||
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).