Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: isDeepStrictEqual always returns false to compare WeakMap/WeakSet #18228

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/internal/util/comparisons.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-util-isDeepStrictEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}