Node.js Reference ยท util, assert and the Test Runner
Node.js util.isDeepStrictEqual() function
Reference for util.isDeepStrictEqual() in Node.js: how deep strict comparison treats NaN, prototypes, Map, Set, Date, symbols and key order, versus JSON or ==
util.isDeepStrictEqual(a, b) returns true when two values are deeply equal under the rules of strict equality, and false otherwise. It never throws for unequal values; it is the boolean version of assert.deepStrictEqual().
Use it when you need to compare structures without a test framework: detecting whether a config changed, deduplicating objects, or checking that a cache entry matches fresh data.
import { isDeepStrictEqual } from "node:util"; isDeepStrictEqual({ a: [1, 2] }, { a: [1, 2] }); // true
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
util.isDeepStrictEqual(val1: any, val2: any): boolean
| Name | Description |
|---|---|
val1, val2 |
The two values to compare. |
Returns: true if they are deeply strictly equal.
Rules in short: primitives are compared with Object.is() (so NaN equals NaN, but 0 does not equal -0); prototypes must match; own enumerable string keys and enumerable symbol keys must match; Map, Set, Date, RegExp, Error and typed arrays are compared by their contents; circular references are handled. Object key order is irrelevant; array order is not.
Usage notes
0and-0are different here, unlike0 === -0. This surprises people who expect===semantics for numbers.undefinedproperties count.{ a: undefined }is not equal to{}; this is also why it differs from comparingJSON.stringify()output.- Own enumerable properties only. Non-enumerable properties and private
#fieldsare not compared. - Class instances must share a prototype. Two objects with equal fields but different classes are unequal; this is the main difference from the loose
assert.deepEqual(). Errorcomparisons includemessageandname;causeanderrorsare compared as well.SetandMapare order-independent, and object members are compared deeply regardless of order.- Functions are compared by identity (
===) like any other reference value; two copies of the same function source are not equal. - Cost is proportional to the size of both structures. Do not call it in a hot loop on large objects; compare a version number or hash instead.
- Unequal-diff details: if you want a readable explanation of why values differ, use
assert.deepStrictEqual()and read the error message.