Skip to content

Conversation

@AbdelrahmanHafez
Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez commented Dec 4, 2025

Summary

  • Add utils.hasOwnKeys() helper that checks for empty objects without allocating an array via Object.keys()
  • Replace ~50 instances of Object.keys(obj).length pattern with hasOwnKeys(), reading the first key only instead of creating an array for all keys.
  • Convert deep property access chains like a && a.b && a.b.c to optional chaining a?.b?.c
  • Cache repeated property lookups (e.g., this.$__schema.options, model.db.options)
  • Optimize indexOf() calls by caching the result instead of calling twice
  • Convert arguments to rest parameters in Query.prototype.slice()
  • Optimize Set operations in document.overwrite() and getModelsMapForPopulate()

@AbdelrahmanHafez AbdelrahmanHafez marked this pull request as draft December 4, 2025 02:24
@AbdelrahmanHafez AbdelrahmanHafez marked this pull request as ready for review December 4, 2025 03:13
Copilot finished reviewing on behalf of AbdelrahmanHafez December 4, 2025 03:17
Copy link
Contributor

Copilot AI left a 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 than Object.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 arguments object 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants