diff --git a/lib/internal/util/comparisons.js b/lib/internal/util/comparisons.js index 1145bc7d9971ba..679b80470190fc 100644 --- a/lib/internal/util/comparisons.js +++ b/lib/internal/util/comparisons.js @@ -80,6 +80,12 @@ function strictDeepEqual(val1, val2, memos) { if (Object.getPrototypeOf(val1) !== Object.getPrototypeOf(val2)) { return false; } + if (val1Tag === '[object WeakMap]' || val1Tag === '[object WeakSet]') { + // Note: Weak Reference Collection is not designed to check the equivalence. + // They are depends on Garbage Collection implementation. + // see: https://tc39.github.io/ecma262/#sec-weakmap-objects + return false; + } if (val1Tag === '[object Array]') { // Check for sparse arrays and general fast path if (val1.length !== val2.length) diff --git a/test/parallel/test-util-isDeepStrictEqual.js b/test/parallel/test-util-isDeepStrictEqual.js index 356a9a71324971..354c43b5ae4b59 100644 --- a/test/parallel/test-util-isDeepStrictEqual.js +++ b/test/parallel/test-util-isDeepStrictEqual.js @@ -481,3 +481,22 @@ utilIsDeepStrict(-0, -0); boxedStringA[symbol1] = true; utilIsDeepStrict(a, b); } + +// Handle WeakMap and WeakSet +{ + const wm1 = new WeakMap(); + const wm2 = new WeakMap(); + const key = {}; + wm1.set(key, 1); + wm2.set(key, 1); + // seems to be equal but WeakMap is always false + notUtilIsDeepStrict(wm1, wm2); + + const ws1 = new WeakSet(); + const ws2 = new WeakSet(); + const obj = {}; + ws1.add(obj); + ws2.add(obj); + // seems to be equal but WeakSet is always false + notUtilIsDeepStrict(ws1, ws2); +}