Skip to content

assert,util: improve deep object comparison performance #57648

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

Merged
Merged
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
2 changes: 1 addition & 1 deletion benchmark/assert/deepequal-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function benchmark(method, n, values, values2) {
}

function main({ n, len, method, strict }) {
const array = Array(len).fill(1);
const array = Array.from({ length: len }, () => '');

switch (method) {
case 'deepEqual_primitiveOnly': {
Expand Down
1 change: 0 additions & 1 deletion benchmark/assert/deepequal-prims-and-objs-big-loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const primValues = {
'number': 1_000,
'boolean': true,
'object': { property: 'abcdef' },
'object_other_property': { property: 'abcdef' },
'array': [1, 2, 3],
'set_object': new Set([[1]]),
'set_simple': new Set([1, 2, 3]),
Expand Down
41 changes: 30 additions & 11 deletions benchmark/assert/deepequal-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ const { deepEqual, deepStrictEqual, notDeepEqual, notDeepStrictEqual } =

const bench = common.createBenchmark(main, {
n: [1e3],
len: [5e2],
len: [2, 1e2],
strict: [0, 1],
order: ['insert', 'random', 'reversed'],
method: [
'deepEqual_primitiveOnly',
'deepEqual_objectOnly',
Expand All @@ -16,12 +17,30 @@ const bench = common.createBenchmark(main, {
'notDeepEqual_objectOnly',
'notDeepEqual_mixed',
],
}, {
combinationFilter(p) {
return p.order !== 'random' || p.strict === 1 && p.method !== 'notDeepEqual_objectOnly';
},
});

function benchmark(method, n, values, values2) {
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}

function benchmark(method, n, values, values2, order) {
const actual = new Set(values);
// Prevent reference equal elements
const deepCopy = JSON.parse(JSON.stringify(values2 ? values2 : values));
let deepCopy = JSON.parse(JSON.stringify(values2));
if (order === 'reversed') {
deepCopy = deepCopy.reverse();
} else if (order === 'random') {
shuffleArray(deepCopy);
}
const expected = new Set(deepCopy);
bench.start();
for (let i = 0; i < n; ++i) {
Expand All @@ -30,39 +49,39 @@ function benchmark(method, n, values, values2) {
bench.end(n);
}

function main({ n, len, method, strict }) {
const array = Array(len).fill(1);
function main({ n, len, method, strict, order }) {
const array = Array.from({ length: len }, () => '');

switch (method) {
case 'deepEqual_primitiveOnly': {
const values = array.map((_, i) => `str_${i}`);
benchmark(strict ? deepStrictEqual : deepEqual, n, values);
benchmark(strict ? deepStrictEqual : deepEqual, n, values, values, order);
break;
}
case 'deepEqual_objectOnly': {
const values = array.map((_, i) => [`str_${i}`, null]);
benchmark(strict ? deepStrictEqual : deepEqual, n, values);
benchmark(strict ? deepStrictEqual : deepEqual, n, values, values, order);
break;
}
case 'deepEqual_mixed': {
const values = array.map((_, i) => {
return i % 2 ? [`str_${i}`, null] : `str_${i}`;
});
benchmark(strict ? deepStrictEqual : deepEqual, n, values);
benchmark(strict ? deepStrictEqual : deepEqual, n, values, values, order);
break;
}
case 'notDeepEqual_primitiveOnly': {
const values = array.map((_, i) => `str_${i}`);
const values2 = values.slice(0);
values2[Math.floor(len / 2)] = 'w00t';
benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2);
benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2, order);
break;
}
case 'notDeepEqual_objectOnly': {
const values = array.map((_, i) => [`str_${i}`, null]);
const values2 = values.slice(0);
values2[Math.floor(len / 2)] = ['w00t'];
benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2);
benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2, order);
break;
}
case 'notDeepEqual_mixed': {
Expand All @@ -71,7 +90,7 @@ function main({ n, len, method, strict }) {
});
const values2 = values.slice();
values2[0] = 'w00t';
benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2);
benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2, order);
break;
}
default:
Expand Down
2 changes: 1 addition & 1 deletion benchmark/assert/partial-deep-equal.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function createSets(length, extraProps, depth = 0) {
number: i,
},
['array', 'with', 'values'],
!depth ? new Set([1, 2, { nested: i }]) : new Set(),
!depth ? new Set([1, { nested: i }]) : new Set(),
!depth ? createSets(2, extraProps, depth + 1) : null,
]));
}
Expand Down
Loading
Loading