-
-
Notifications
You must be signed in to change notification settings - Fork 4k
perf: optimize Object.keys() checks and optional chaining #15865
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
Open
AbdelrahmanHafez
wants to merge
2
commits into
master
Choose a base branch
from
perf/optimize-object-keys
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+175
−196
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Contributor
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.
Pull request overview
This PR implements several performance optimizations across the Mongoose codebase:
- Introduces a new
utils.hasOwnKeys()helper that checks for object keys more efficiently thanObject.keys().length - Converts verbose property access chains to optional chaining syntax
- Caches repeated property lookups to avoid redundant access
- Optimizes
indexOf()calls by caching results - Converts
argumentsobject usage to rest parameters - Optimizes Set operations for duplicate checking
Reviewed changes
Copilot reviewed 35 out of 35 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| lib/utils.js | Adds hasOwnKeys() helper function and updates isEmptyObject() to use it |
| lib/types/subdocument.js | Replaces Object.keys(ret).length === 0 with hasOwnKeys() |
| lib/types/documentArray/methods/index.js | Converts property access chains to optional chaining |
| lib/types/documentArray/index.js | Converts property access chains to optional chaining |
| lib/types/array/methods/index.js | Replaces Object.keys() length checks with hasOwnKeys() |
| lib/types/array/index.js | Converts property access chains to optional chaining |
| lib/schemaType.js | Simplifies null/undefined check to != null |
| lib/schema/subdocument.js | Converts to optional chaining and uses hasOwnKeys() |
| lib/schema/string.js | Simplifies null/undefined checks to != null |
| lib/schema/number.js | Simplifies null/undefined checks to != null |
| lib/schema/mixed.js | Replaces Object.keys() check with hasOwnKeys() |
| lib/schema/map.js | Replaces Object.keys() check with hasOwnKeys() |
| lib/schema/array.js | Converts property access chain to optional chaining |
| lib/schema.js | Converts to optional chaining and uses hasOwnKeys() |
| lib/queryHelpers.js | Converts to optional chaining |
| lib/query.js | Caches repeated property access, converts to rest parameters, uses hasOwnKeys(), and optional chaining |
| lib/plugins/trackTransaction.js | Replaces Object.keys() checks with hasOwnKeys() |
| lib/mongoose.js | Converts to optional chaining and uses hasOwnKeys() |
| lib/model.js | Converts to optional chaining and uses hasOwnKeys() |
| lib/helpers/update/castArrayFilters.js | Adds utils import and uses hasOwnKeys() |
| lib/helpers/update/applyTimestampsToUpdate.js | Adds utils import and uses hasOwnKeys() |
| lib/helpers/schema/getIndexes.js | Simplifies null/undefined check to != null |
| lib/helpers/schema/applyWriteConcern.js | Adds utils import, uses optional chaining and hasOwnKeys() |
| lib/helpers/schema/applyReadConcern.js | Converts to optional chaining |
| lib/helpers/query/castUpdate.js | Uses hasOwnKeys() for empty object checks |
| lib/helpers/populate/getModelsMapForPopulate.js | Optimizes duplicate checking with Set, uses optional chaining and hasOwnKeys() |
| lib/helpers/document/compile.js | Converts to optional chaining and uses hasOwnKeys() |
| lib/helpers/document/applyVirtuals.js | Adds utils import and uses hasOwnKeys() |
| lib/helpers/document/applyTimestamps.js | Adds utils import and uses hasOwnKeys() |
| lib/drivers/node-mongodb-native/connection.js | Converts to optional chaining and uses hasOwnKeys() |
| lib/drivers/node-mongodb-native/collection.js | Converts to optional chaining |
| lib/document.js | Optimizes with Set for key deduplication, caches schema options, uses hasOwnKeys() |
| lib/cursor/changeStream.js | Converts to optional chaining |
| lib/connection.js | Caches indexOf() results and uses hasOwnKeys() |
| lib/cast.js | Converts to optional chaining and uses hasOwnKeys() |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
hasezoey
approved these changes
Dec 4, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
utils.hasOwnKeys()helper that checks for empty objects without allocating an array viaObject.keys()Object.keys(obj).lengthpattern withhasOwnKeys(), reading the first key only instead of creating an array for all keys.a && a.b && a.b.cto optional chaininga?.b?.cthis.$__schema.options,model.db.options)indexOf()calls by caching the result instead of calling twiceargumentsto rest parameters inQuery.prototype.slice()Setoperations indocument.overwrite()andgetModelsMapForPopulate()