Utility Functions
GlyphUI exports utility functions that are used internally for virtual DOM diffing and can also be used in your application code for object comparisons.isShallowEqual()
Performs a shallow equality check between two objects. Returnstrue if both objects have the same keys and values, where values are compared with strict equality (===).
First object to compare
Second object to compare
true if objects are shallowly equal, false otherwiseBehavior
- Returns
trueif both references are identical (obj1 === obj2) - Returns
falseif either object isnullorundefined - Compares the number of keys in both objects
- Compares each key’s value using strict equality (
===) - Special handling for
onproperty (event listeners) - only checks existence - Skips the
keyproperty in comparison
Usage
This function is used internally by GlyphUI’s virtual DOM diffing algorithm to determine if component props have changed.
isDeepEqual()
Performs a deep equality check between two values. Recursively compares nested objects and arrays.First value to compare
Second value to compare
true if values are deeply equal, false otherwiseBehavior
- Handles primitives (strings, numbers, booleans)
- Returns
trueif both references are identical - Returns
falseif either value isnullorundefined - Returns
falseif types don’t match - Recursively compares arrays element by element
- Recursively compares object properties
- Skips function comparisons (always returns
truefor function properties)
Usage
This function is primarily useful for testing. It’s not used in the core virtual DOM diffing algorithm since shallow comparison is sufficient for most cases.
When to Use
Use isShallowEqual
- Comparing component props
- Checking if re-render is needed
- Performance-critical paths
- Flat object structures
Use isDeepEqual
- Testing deeply nested structures
- Verifying complex state changes
- Non-performance-critical code
- Debugging and assertions
Performance Considerations
- Shallow comparison is O(n) where n is the number of keys
- Deep comparison can be O(n×m) for nested structures
- For performance-critical code, prefer shallow comparison
- Deep comparison should be used sparingly in production code
Related
- Virtual DOM - Learn how these utilities are used in diffing
- Rendering - Understand the reconciliation process
- Performance - Optimization techniques