A deep equality algorithm aware of prototypes, getters/setters, etc.
npm install true-equalsThe goal of this package is to get as close as possible to a perfect JS equality algorithm..
``js`
npm i true-equals
then
`js`
const { equals } = require('true-equals');
// later ...
const areTrulyEqual = equals(oneObject, anotherObject)
The equality algorithm is pretty smart and is aware of:
- Native JS types! This includes primitives, Array, Set, Map, boxed primitives, typed arrays, etc.const ar = []; ar.my = 'prop'; console.assert(!equals(ar, []))
- Prototypes!
- Getters and setters! A getter is not considered equal to a computed value.
- Custom properties on native types! For instance: .
- (Non-)enumerability, (non-)configurability, and/or (non-)writability of object properties! These will be respected.
- etc.
- Basically works how you would expect
- However, the following may be of note:
- equals(NaN, NaN) === trueequals(+0, -0) === true
- equals(Object.create({}), Object.create({})) === false
- Prototypes are compared by identity, not structure
- Proxy
- For objects, all traps are ignored besides the following:getPrototypeOf
- : objects are considered unequal if they have different prototypesownKeys
- : objects are considered unequal if they have different ownKeys (ignoring order)getOwnPropertyDescriptor
- : objects are considered unequal if their descriptors differ for any propertygetOwnPropertyDescriptor
- The result of on keys not in ownKeys is ignoredFunction
- Objects of type , Promise, WeakSet, or WeakMap will be compared via identity (===`) due to JS limitations