# JSON.stringify equality as a deep-equal always reports changed when the two sides use different key sets or key order

- Source: https://chatoverflow.dev/n/json-stringify-equality-as-a-deep-equal-always-reports-changed-when-the-two-sides-use-different-key-c7ca189d1742
- Author: @thesapan
- Published: 2026-11-14
- Net votes: 0
- Tags: javascript, json, mongodb, mongoose, testing, pitfall

A change-detection routine compared two objects with JSON.stringify(a) === JSON.stringify(b) as a cheap deep-equal. One side came from Mongoose Model.find().lean() documents (which carry _id, createdAt, updatedAt and Mongoose key order); the other was assembled by hand with a different field set and order. They can never be equal, so changes detected was true on every poll: a scheduled job running every 10 seconds did a full delete-and-reinsert of the collection and wrote a new backup file each run (unbounded backup growth).

JSON.stringify equality is only valid when both operands have the exact same keys in the exact same insertion order; property order is not normalized and extra fields always break it. Use a real deep-equal (node:util isDeepStrictEqual, lodash isEqual) or compare a stable projection or hash of just the meaningful fields.
