-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
doc: WeakSet
and WeakMap
comparison details
#56648
Merged
nodejs-github-bot
merged 3 commits into
nodejs:main
from
ishon19:56640-outdated-comparison-weakset-and-weakmap
Jan 20, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -804,8 +804,10 @@ are recursively evaluated also by the following rules. | |||||||||||
* [`Map`][] keys and [`Set`][] items are compared unordered. | ||||||||||||
* Recursion stops when both sides differ or both sides encounter a circular | ||||||||||||
reference. | ||||||||||||
* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values. See | ||||||||||||
below for further details. | ||||||||||||
* [`WeakMap`][] and [`WeakSet`][] instances are **not** compared structurally. | ||||||||||||
They are only equal if they reference the same object. Any comparison between | ||||||||||||
different `WeakMap` or `WeakSet` instances will result in inequality, | ||||||||||||
even if they contain the same entries. | ||||||||||||
* [`RegExp`][] lastIndex, flags, and source are always compared, even if these | ||||||||||||
are not enumerable properties. | ||||||||||||
|
||||||||||||
|
@@ -882,23 +884,40 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 }); | |||||||||||
// } | ||||||||||||
|
||||||||||||
const weakMap1 = new WeakMap(); | ||||||||||||
const weakMap2 = new WeakMap([[{}, {}]]); | ||||||||||||
const weakMap3 = new WeakMap(); | ||||||||||||
weakMap3.unequal = true; | ||||||||||||
const weakMap2 = new WeakMap(); | ||||||||||||
const obj = {}; | ||||||||||||
|
||||||||||||
weakMap1.set(obj, 'value'); | ||||||||||||
weakMap2.set(obj, 'value'); | ||||||||||||
|
||||||||||||
// Comparing different instances fails, even with same contents | ||||||||||||
assert.deepStrictEqual(weakMap1, weakMap2); | ||||||||||||
// OK, because it is impossible to compare the entries | ||||||||||||
// AssertionError: Values have same structure but are not reference-equal: | ||||||||||||
// | ||||||||||||
// WeakMap { | ||||||||||||
// <items unknown> | ||||||||||||
// } | ||||||||||||
|
||||||||||||
// Comparing the same instance to itself succeeds | ||||||||||||
assert.deepStrictEqual(weakMap1, weakMap1); | ||||||||||||
// OK | ||||||||||||
|
||||||||||||
// Fails because weakMap3 has a property that weakMap1 does not contain: | ||||||||||||
assert.deepStrictEqual(weakMap1, weakMap3); | ||||||||||||
const weakSet1 = new WeakSet(); | ||||||||||||
const weakSet2 = new WeakSet(); | ||||||||||||
weakSet1.add(obj); | ||||||||||||
weakSet2.add(obj); | ||||||||||||
|
||||||||||||
// Comparing different instances fails, even with same contents | ||||||||||||
assert.deepStrictEqual(weakSet1, weakSet2); | ||||||||||||
// AssertionError: Expected inputs to be strictly deep-equal: | ||||||||||||
// + actual - expected | ||||||||||||
// | ||||||||||||
// WeakMap { | ||||||||||||
// + [items unknown] | ||||||||||||
// - [items unknown], | ||||||||||||
// - unequal: true | ||||||||||||
// } | ||||||||||||
// + WeakSet { <items unknown> } | ||||||||||||
// - WeakSet { <items unknown> } | ||||||||||||
Comment on lines
+915
to
+916
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
// Comparing the same instance to itself succeeds | ||||||||||||
assert.deepStrictEqual(weakSet1, weakSet1); | ||||||||||||
// OK | ||||||||||||
``` | ||||||||||||
|
||||||||||||
```cjs | ||||||||||||
|
@@ -974,23 +993,40 @@ assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 }); | |||||||||||
// } | ||||||||||||
|
||||||||||||
const weakMap1 = new WeakMap(); | ||||||||||||
const weakMap2 = new WeakMap([[{}, {}]]); | ||||||||||||
const weakMap3 = new WeakMap(); | ||||||||||||
weakMap3.unequal = true; | ||||||||||||
const weakMap2 = new WeakMap(); | ||||||||||||
const obj = {}; | ||||||||||||
|
||||||||||||
weakMap1.set(obj, 'value'); | ||||||||||||
weakMap2.set(obj, 'value'); | ||||||||||||
|
||||||||||||
// Comparing different instances fails, even with same contents | ||||||||||||
assert.deepStrictEqual(weakMap1, weakMap2); | ||||||||||||
// OK, because it is impossible to compare the entries | ||||||||||||
// AssertionError: Values have same structure but are not reference-equal: | ||||||||||||
// | ||||||||||||
// WeakMap { | ||||||||||||
// <items unknown> | ||||||||||||
// } | ||||||||||||
|
||||||||||||
// Comparing the same instance to itself succeeds | ||||||||||||
assert.deepStrictEqual(weakMap1, weakMap1); | ||||||||||||
// OK | ||||||||||||
|
||||||||||||
// Fails because weakMap3 has a property that weakMap1 does not contain: | ||||||||||||
assert.deepStrictEqual(weakMap1, weakMap3); | ||||||||||||
const weakSet1 = new WeakSet(); | ||||||||||||
const weakSet2 = new WeakSet(); | ||||||||||||
weakSet1.add(obj); | ||||||||||||
weakSet2.add(obj); | ||||||||||||
|
||||||||||||
// Comparing different instances fails, even with same contents | ||||||||||||
assert.deepStrictEqual(weakSet1, weakSet2); | ||||||||||||
// AssertionError: Expected inputs to be strictly deep-equal: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
// + actual - expected | ||||||||||||
// | ||||||||||||
// WeakMap { | ||||||||||||
// + [items unknown] | ||||||||||||
// - [items unknown], | ||||||||||||
// - unequal: true | ||||||||||||
// } | ||||||||||||
// + WeakSet { <items unknown> } | ||||||||||||
// - WeakSet { <items unknown> } | ||||||||||||
Comment on lines
+1024
to
+1025
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
// Comparing the same instance to itself succeeds | ||||||||||||
assert.deepStrictEqual(weakSet1, weakSet1); | ||||||||||||
// OK | ||||||||||||
``` | ||||||||||||
|
||||||||||||
If the values are not equal, an [`AssertionError`][] is thrown with a `message` | ||||||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.