Skip to main content

Deep Equality Checker

Performs a deep comparison between two values (arrays, objects, and primitives) to determine if they are structurally and value-wise equal.
This function is one of the foundational utilities, used internally throughout the nhb-toolbox package to compare arrays, objects, or any deeply nested structures.


Import​

import { isDeepEqual } from 'nhb-toolbox';

Function Signature​

function isDeepEqual<T>(a: T, b: T): boolean

Usage Examples​

isDeepEqual(42, 42)
// true

isDeepEqual('hello', 'world')
// false

API Reference​

Parameters​

NameTypeDescription
aTFirst value to compare.
bTSecond value to compare.

Returns​

  • true if the values are deeply equal.
  • false otherwise.

How it Works​

  • Strict Equality First: If a and b are the same reference or primitive value, returns true immediately.
  • Type & Null Checks: If types differ or either value is null, returns false unless both are strictly equal.
  • Array Handling: Arrays compared recursively, including nested arrays.
  • Object Handling: Objects compared recursively, ensuring both keys and values match exactly.
  • Shallow/Deep: Works for deeply nested arrays/objects as well as simple values.

Key Features​

  • Recursive & Robust: Handles arbitrary nesting for arrays and objects.
  • Type Safety: Works generically for any consistent types.
  • Foundational Utility: This deep comparison utility powers internal checks across many toolbox functions (e.g., deduplication, merging, diffing, and more).

Limitations​

  • No Cycles Supported: Will stack overflow on circular references.
  • No Symbol/Function/Date Support: Only works for plain objects, arrays, and primitives. Special types (functions, Dates, Maps, Sets, Symbols) are not deeply compared.
  • No Non-Enumerable or Prototype Chain: Only owns keys compared, prototype chain & non-enumerable properties are ignored.
  • No Partial/Object Subset Comparison: Strict equality over all keys and values only.

Notes​

  • If you compare objects with arrays, types, or keys in different order but same values, equality will follow JavaScript’s structure (order matters in arrays, not in objects).
  • For performance sensitive code, avoid using on excessively large or deeply nested structures unless necessary.
  • This function is extensively used internally in nhb-toolbox for object/array equality checks.

  • Deduplicating structurally equal objects/arrays.
  • Testing and assertion helpers.
  • Data merging, diffing, or synchronization.
  • Safely comparing complex API payloads or configurations.

Conclusion:
isDeepEqual is a robust, recursive deep comparison tool for reliably testing equality of complex JavaScript values.
Its reliability and correctness make it a key foundation internally across many nhb-toolbox features.