Skip to content

Conversation

@yogesh968
Copy link

Optimize deepEqual & Fix Unnecessary Time Complexity

🔧 What Was Changed

1️⃣ Improved lib/utils.js

  • Removed a redundant for loop that checked whether two objects had keys in the exact same order.
  • This check introduced an unnecessary O(N) time complexity.
  • In standard deep equality comparisons, key order does not matter.
    Example:
    { a: 1, b: 2 } === { b: 2, a: 1 }
  • By removing the loop:
    • The function became faster

    • The logic became cleaner

    • Behavior now aligns with typical deep equality expectations

2️⃣ Updated test/utils.test.js

  • A test previously expected deepEqual to return false when key order differed.
  • Since key order is no longer a factor, deepEqual now correctly returns true.
  • The test was updated to reflect the correct behavior.

2️⃣ Updated test/utils.test.js

  • I have done better formatting of code adding some whitespaces and remove the extra-spaces.

🚀 Why This Matters

  • Eliminates unnecessary computation
  • Avoids misleading equality results
  • Makes the utility more predictable and consistent
  • Improves overall performance without changing any functional outcomes

Result

  • Correct deep equality behavior
  • Reduced time complexity
  • Cleaner, more maintainable code

@yogesh968
Copy link
Author

yogesh968 commented Nov 24, 2025

@wvl @svperfecta @Sija @lackac @vkarpov15 can you please see it ..

@yogesh968
Copy link
Author

Hi team 👋

I’ve submitted an optimization to deepEqual that improves performance and aligns with standard object equality behavior. All tests have been updated accordingly.

Whenever you get a chance, could someone please review and approve the workflows so the checks can run?

Thank you!

@vkarpov15
Copy link
Collaborator

  1. There are certain cases where key order does matter in Mongoose, for example when defining indexes: schema.index({ name: 1, age: 1 }) is a different index than schema.index({ age: 1, name: 1 })
  2. Please don't commit all these formatting changes, that makes code review more difficult

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

2 participants