shallow
The shallow
function is used to perform a shallow comparison between two objects. It checks if the objects are identical in terms of structure and values, including nested objects. It can be used to determine if two objects are equivalent, accounting for references and deep equality.
Usage
The function compares two objects (or values) to see if they are equivalent based on their properties. It works by comparing the objects at all levels of nesting, ensuring that they have the same properties and values.
Parameters
objA
: The first object to compare. It can be any object or value.objB
: The second object to compare. It can also be any object or value.
Return Value
boolean
: Returns true if both objects are shallowly equivalent, and false if they are not.
How It Works
- Object Reference Check: Initially, the function checks if both objects are the same reference using Object.is(). If they are, it returns true.
- Type and Null Check: If either of the objects is not an object or is null, the function immediately returns false.
- Deep Comparison: For non-primitive objects, the function recursively compares their keys and values. It ensures that both objects have the same structure and equivalent values, including deep nested properties.
- WeakMap for Cyclic References: The function uses a WeakMap to track objects that have already been compared, avoiding infinite loops in case of cyclic references.