-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
assert: improve deepEqual perf for large input #12849
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -285,15 +285,24 @@ function _deepEqual(actual, expected, strict, memos) { | |
// Note: this accounts for both named and indexed properties on Arrays. | ||
|
||
// Use memos to handle cycles. | ||
memos = memos || { actual: [], expected: [] }; | ||
const actualIndex = memos.actual.indexOf(actual); | ||
if (actualIndex !== -1) { | ||
if (actualIndex === memos.expected.indexOf(expected)) { | ||
if (!memos) { | ||
memos = { | ||
actual: { map: new Map(), position: 0 }, | ||
expected: { map: new Map(), position: 0 } | ||
}; | ||
} | ||
|
||
const actualPosition = memos.actual.map.get(actual); | ||
if (actualPosition !== undefined) { | ||
if (actualPosition === memos.expected.map.get(expected)) { | ||
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. Changing from Array.prototype.indexOf (which uses Strict Equality Comparison) to Map.prototype.get (which uses SameValueZero) can have subtle nuances that might result in incompatibilities, especially around NaN. Not sure if we need to care about it though. 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. I don’t think that actually affects the logic here – if it matters at all, it should only affect cases where non-primitives compare deep-equal to primitives, but that doesn’t seem to happen anyway (e.g. |
||
return true; | ||
} | ||
} else { | ||
memos.actual.map.set(actual, memos.actual.position++); | ||
} | ||
if (!memos.expected.map.has(expected)) { | ||
memos.expected.map.set(expected, memos.expected.position++); | ||
} | ||
memos.actual.push(actual); | ||
memos.expected.push(expected); | ||
|
||
return objEquiv(actual, expected, strict, memos); | ||
} | ||
|
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.
How about a
WeakMap
?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.
@joyeecheung That thought occurred to me and I couldn’t really find a difference … would there be one? Is that going to be more performant?
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.
@addaleax I think the GC pressure would be smaller after we leave the function because the references don't have be traversed?
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.
@joyeecheung ok, updated :)