Repository: nodejs/node
Pull request: assert,util: handle invalid dates as equal in deep comparison

Invalid dates are now handled as equal in all deep comparisons.

This is more of a usability improvement than a breaking change, since all invalid dates have always thrown errors when being compared until now.

Changed files:
filename: benchmark/assert/deepequal-prims-and-objs-big-loop.js
patch: @@ -22,6 +22,7 @@ const primValues = {
   'empty_object': {},
   'regexp': /abc/i,
   'date': new Date(),
+  'invalidDate': new Date('foo'),
 };
 
 const primValues2 = {
@@ -34,6 +35,7 @@ const primValues2 = {
   'empty_object': {},
   'regexp': /abc/i,
   'date': new Date(primValues.date),
+  'invalidDate': new Date('foo'),
 };
 
 const primValuesUnequal = {
@@ -49,6 +51,7 @@ const primValuesUnequal = {
   'empty_object': [],
   'regexp': /abc/g,
   'date': new Date(primValues.date.getTime() + 1),
+  'invalidDate': new Date(),
 };
 
 const bench = common.createBenchmark(main, {

filename: doc/api/assert.md
patch: @@ -231,6 +231,9 @@ An alias of [`assert.ok()`][].
 <!-- YAML
 added: v0.1.21
 changes:
+  - version: REPLACEME
+    pr-url: https://github.com/nodejs/node/pull/57627
+    description: Invalid dates are now considered equal.
   - version: v24.0.0
     pr-url: https://github.com/nodejs/node/pull/57622
     description: Recursion now stops when either side encounters a circular
@@ -422,6 +425,9 @@ parameter is an instance of {Error} then it will be thrown instead of the
 <!-- YAML
 added: v1.2.0
 changes:
+  - version: REPLACEME
+    pr-url: https://github.com/nodejs/node/pull/57627
+    description: Invalid dates are now considered equal.
   - version: v24.0.0
     pr-url: https://github.com/nodejs/node/pull/57622
     description: Recursion now stops when either side encounters a circular
@@ -2177,9 +2183,12 @@ added:
   - v23.4.0
   - v22.13.0
 changes:
- - version: v24.0.0
-   pr-url: https://github.com/nodejs/node/pull/57370
-   description: partialDeepStrictEqual is now Stable. Previously, it had been Experimental.
+  - version: REPLACEME
+    pr-url: https://github.com/nodejs/node/pull/57627
+    description: Invalid dates are now considered equal.
+  - version: v24.0.0
+    pr-url: https://github.com/nodejs/node/pull/57370
+    description: partialDeepStrictEqual is now Stable. Previously, it had been Experimental.
 -->
 
 * `actual` {any}

filename: lib/internal/util/comparisons.js
patch: @@ -299,10 +299,15 @@ function objectComparisonStart(val1, val2, mode, memos) {
   } else if (val1Tag === '[object Object]') {
     return keyCheck(val1, val2, mode, memos, kNoIterator);
   } else if (isDate(val1)) {
-    if (!isDate(val2) ||
-        DatePrototypeGetTime(val1) !== DatePrototypeGetTime(val2)) {
+    if (!isDate(val2)) {
       return false;
     }
+    const time1 = DatePrototypeGetTime(val1);
+    const time2 = DatePrototypeGetTime(val2);
+    if (time1 !== time2) {
+      // eslint-disable-next-line no-self-compare
+      return time1 !== time1 && time2 !== time2;
+    }
   } else if (isRegExp(val1)) {
     if (!isRegExp(val2) || !areSimilarRegExps(val1, val2)) {
       return false;

filename: test/parallel/test-assert-deep.js
patch: @@ -752,6 +752,8 @@ test('Additional tests', () => {
 
   assertNotDeepOrStrict(new Date(), new Date(2000, 3, 14));
 
+  assertDeepAndStrictEqual(new Date('foo'), new Date('bar'));
+
   assertDeepAndStrictEqual(/a/, /a/);
   assertDeepAndStrictEqual(/a/g, /a/g);
   assertDeepAndStrictEqual(/a/i, /a/i);