assert: improve deepEqual perf for large input
Use a Map instead of an array for checking previously found cyclic references. This reduces complexity for an array-of-objects case from O(n²) to O(n·log n). Fixes: https://github.com/nodejs/node/issues/12842 PR-URL: https://github.com/nodejs/node/pull/12849 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
parent
3fd890a06e
commit
7e5f500c98
@ -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)) {
|
||||
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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user