Skip to content

Conversation

@manak-sharma20
Copy link

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:

  • plain object (not an array)
  • no operator keys ($...)
  • values are primitives (string, number, boolean), RegExp, or null
  • the matching schema paths are not ObjectId fields (to avoid skipping required ObjectId casting)

If all conditions are satisfied, _castConditions returns 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:

Model.find({ name: "Alice" })
Model.findOne({ email: "[email protected]" })

@manak-sharma20
Copy link
Author

@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);
Copy link
Contributor

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 ?

Copy link
Author

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).

Copy link
Collaborator

@vkarpov15 vkarpov15 left a 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;
Copy link
Collaborator

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;
Copy link
Collaborator

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.

@vkarpov15
Copy link
Collaborator

I'm going to close this PR since I don't think it is salvageable.

@vkarpov15 vkarpov15 closed this Nov 26, 2025
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.

3 participants