Object.isEqual() - Reference Examples

Practical examples demonstrating the capabilities of Object.isEqual v5.1.0.

1. Primitives and Special Values

NaN Equality

Object.isEqual(NaN, NaN); // true
true

Unlike ===, NaN is equal to itself.

Zero vs Negative Zero

Object.isEqual(0, -0); // true (default)
Object.isEqual(0, -0, { strict: true }); // false
true / false

2. Objects and Nested Structures

Simple Object

Object.isEqual({name:'Alice',age:30},{name:'Alice',age:30}); // true
true

Circular References

var a={}; a.self=a;
var b={}; b.self=b;
Object.isEqual(a, b); // true
true

3. Map and Set

Map with Object Keys

var m1=new Map([[{id:1},'val']]);
var m2=new Map([[{id:1},'val']]);
Object.isEqual(m1, m2); // true
true

Set Comparison

Object.isEqual(new Set([1,2,3]),new Set([3,2,1])); // true
true

Order does not matter.

4. Date and RegExp

Date Comparison

Object.isEqual(new Date('2025-01-01'),new Date('2025-01-01')); // true
true

RegExp

Object.isEqual(/abc/gi, /abc/gi); // true
Object.isEqual(/abc/gi, /abc/g); // false
true / false

5. Custom Comparators

Per-Key Ignore

Object.isEqual({id:1,ts:100},{id:1,ts:200},{customComparators:{ts:()=>true}}); // true
true

Global Comparator

Object.isEqual({a:1,b:'2'},{a:1,b:2},{customComparators:{[Symbol.for('*')]:(x,y)=>x==y}}); // true
true

6. Safe Mode (Security)

Getter That Throws

var obj={get x(){throw new Error('no')},y:1};
Object.isEqual(obj,obj,{safe:true}); // false
false

Exceptions are silently caught.

Without Safe Mode

// Object.isEqual(obj,obj,{safe:false}); // throws Error
throws

Use safe:false for debugging.