perf: optimize Object.keys() checks and optional chaining#15865
Conversation
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 35 out of 35 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
vkarpov15
left a comment
There was a problem hiding this comment.
Some of the changes look slower than what was originally there, most notably optional chaining and checking if Object.keys(obj).length.
|
Changing this to draft since I found a bunch of other opportunities to refactor for optional chaining and simplify code in general. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 83 out of 83 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@vkarpov15 This PR grew a bit larger than I initially intended, there were many places where optional chaining could improve readability and/or performance. I've reviewed it a couple of times myself, had Copilot validate it as well, and all tests and lint are passing, which should give us good confidence in these changes. That said, if it would make things easier for you, I'm happy to split this into smaller PRs. Let me know if there's anything I can do to make the review process smoother. |
|
This PR's great, no need for separate PRs. Thanks 👍 |
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.x && x.y→x?.ypatternsx != null && x.y→x?.ypatternsx && x.y && x.z→x?.y?.zdeep access patternsx != null ? x : default→x ?? defaultArray.isArray()calls (Array.isArray already returns false for null/undefined, and performs the same)this.$__schema.options,model.db.options)indexOf()calls by caching the result instead of calling twiceargumentsto rest parameters inQuery.prototype.slice()Setoperations indocument.overwrite()andgetModelsMapForPopulate()