deepEqual
Recursively compares two values for structural equality. Handles primitives, plain objects, class instances (same prototype), arrays, Date, RegExp, Map, Set, ArrayBuffer, and typed arrays. Circular references are tracked with a WeakMap pair-cache; on cycle, the pair is assumed equal. Functions, Error instances, and other unsupported objects fall back to reference equality.
Try it
Section titled “Try it”Import
Section titled “Import”import { deepEqual } from "1o1-utils";import { deepEqual } from "1o1-utils/deep-equal";Signature
Section titled “Signature”function deepEqual({ a, b }: DeepEqualParams): booleanParameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
| a | unknown | Yes | The first value to compare |
| b | unknown | Yes | The second value to compare |
Returns
Section titled “Returns”boolean — true if the values are deeply equal, false otherwise.
Examples
Section titled “Examples”deepEqual({ a: { x: { n: 1 } }, b: { x: { n: 1 } } });// => true
deepEqual({ a: [1, [2, 3]], b: [1, [2, 3]] });// => true
deepEqual({ a: new Date("2024-01-01"), b: new Date("2024-01-01") });// => true
deepEqual({ a: new Map([["x", 1]]), b: new Map([["x", 1]]) });// => true
deepEqual({ a: { x: 1 }, b: { x: 2 } });// => false// React: skip a render only when the entire props tree is unchangedfunction arePropsEqual(prev, next) { return deepEqual({ a: prev, b: next });}Edge Cases
Section titled “Edge Cases”- Uses
Object.is, soNaNequalsNaNand+0is distinct from-0. - Returns
truefor identical references (fast path). - Same-prototype check runs first — a plain object never equals a class instance with identical fields.
- Symbol-keyed and non-enumerable properties are ignored.
Date,RegExp,ArrayBuffer, and typed arrays compare structurally;Errorinstances fall back to reference equality.MapandSetdeep comparisons use a greedy O(n²) match for elements not found by reference — fine for typical sizes, slow for very large collections.- Circular references are detected with a
WeakMappair-cache; on cycle the pair is assumed equal.
Also known as
Section titled “Also known as”deep compare, structural equality, recursive equal, isEqual, value equality
Prompt suggestion
Section titled “Prompt suggestion”I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use deepEqual to compare two nested config objects for full structural equality