Skip to content
Tutorials Center
My learning

Topics

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.

NODE
import { isDeepStrictEqual } from "node:util";

isDeepStrictEqual({ a: [1, 2] }, { a: [1, 2] }); // true

Try it

Node.js What counts as equal

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.

Node.js Prototypes, properties and types

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

NODE
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

  • 0 and -0 are different here, unlike 0 === -0. This surprises people who expect === semantics for numbers.
  • undefined properties count. { a: undefined } is not equal to {}; this is also why it differs from comparing JSON.stringify() output.
  • Own enumerable properties only. Non-enumerable properties and private #fields are 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().
  • Error comparisons include message and name; cause and errors are compared as well.
  • Set and Map are 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.