Skip to content

Commit f4572f0

Browse files
BridgeARRafaelGSS
authored andcommitted
assert,util: improve deep object comparison performance
This improves the performance for almost all objects when comparing them deeply. PR-URL: #57648 Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Bryan English <bryan@bryanenglish.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent ed72044 commit f4572f0

6 files changed

Lines changed: 336 additions & 167 deletions

File tree

‎benchmark/assert/deepequal-map.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function benchmark(method, n, values, values2) {
3131
}
3232

3333
functionmain({ n, len, method, strict }){
34-
constarray=Array(len).fill(1);
34+
constarray=Array.from({length: len},()=>'');
3535

3636
switch(method){
3737
case'deepEqual_primitiveOnly': {

‎benchmark/assert/deepequal-prims-and-objs-big-loop.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const primValues = {
1414
'number': 1_000,
1515
'boolean': true,
1616
'object': {property: 'abcdef'},
17-
'object_other_property': {property: 'abcdef'},
1817
'array': [1,2,3],
1918
'set_object': newSet([[1]]),
2019
'set_simple': newSet([1,2,3]),

‎benchmark/assert/deepequal-set.js‎

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ const { deepEqual, deepStrictEqual, notDeepEqual, notDeepStrictEqual } =
66

77
constbench=common.createBenchmark(main,{
88
n: [1e3],
9-
len: [5e2],
9+
len: [2,1e2],
1010
strict: [0,1],
11+
order: ['insert','random','reversed'],
1112
method: [
1213
'deepEqual_primitiveOnly',
1314
'deepEqual_objectOnly',
@@ -16,12 +17,30 @@ const bench = common.createBenchmark(main, {
1617
'notDeepEqual_objectOnly',
1718
'notDeepEqual_mixed',
1819
],
20+
},{
21+
combinationFilter(p){
22+
returnp.order!=='random'||p.strict===1&&p.method!=='notDeepEqual_objectOnly';
23+
},
1924
});
2025

21-
functionbenchmark(method,n,values,values2){
26+
functionshuffleArray(array){
27+
for(leti=array.length-1;i>0;i--){
28+
constj=Math.floor(Math.random()*(i+1));
29+
consttemp=array[i];
30+
array[i]=array[j];
31+
array[j]=temp;
32+
}
33+
}
34+
35+
functionbenchmark(method,n,values,values2,order){
2236
constactual=newSet(values);
2337
// Prevent reference equal elements
24-
constdeepCopy=JSON.parse(JSON.stringify(values2 ? values2 : values));
38+
letdeepCopy=JSON.parse(JSON.stringify(values2));
39+
if(order==='reversed'){
40+
deepCopy=deepCopy.reverse();
41+
}elseif(order==='random'){
42+
shuffleArray(deepCopy);
43+
}
2544
constexpected=newSet(deepCopy);
2645
bench.start();
2746
for(leti=0;i<n;++i){
@@ -30,39 +49,39 @@ function benchmark(method, n, values, values2) {
3049
bench.end(n);
3150
}
3251

33-
functionmain({ n, len, method, strict }){
34-
constarray=Array(len).fill(1);
52+
functionmain({ n, len, method, strict, order}){
53+
constarray=Array.from({length: len},()=>'');
3554

3655
switch(method){
3756
case'deepEqual_primitiveOnly': {
3857
constvalues=array.map((_,i)=>`str_${i}`);
39-
benchmark(strict ? deepStrictEqual : deepEqual,n,values);
58+
benchmark(strict ? deepStrictEqual : deepEqual,n,values,values,order);
4059
break;
4160
}
4261
case'deepEqual_objectOnly': {
4362
constvalues=array.map((_,i)=>[`str_${i}`,null]);
44-
benchmark(strict ? deepStrictEqual : deepEqual,n,values);
63+
benchmark(strict ? deepStrictEqual : deepEqual,n,values,values,order);
4564
break;
4665
}
4766
case'deepEqual_mixed': {
4867
constvalues=array.map((_,i)=>{
4968
returni%2 ? [`str_${i}`,null] : `str_${i}`;
5069
});
51-
benchmark(strict ? deepStrictEqual : deepEqual,n,values);
70+
benchmark(strict ? deepStrictEqual : deepEqual,n,values,values,order);
5271
break;
5372
}
5473
case'notDeepEqual_primitiveOnly': {
5574
constvalues=array.map((_,i)=>`str_${i}`);
5675
constvalues2=values.slice(0);
5776
values2[Math.floor(len/2)]='w00t';
58-
benchmark(strict ? notDeepStrictEqual : notDeepEqual,n,values,values2);
77+
benchmark(strict ? notDeepStrictEqual : notDeepEqual,n,values,values2,order);
5978
break;
6079
}
6180
case'notDeepEqual_objectOnly': {
6281
constvalues=array.map((_,i)=>[`str_${i}`,null]);
6382
constvalues2=values.slice(0);
6483
values2[Math.floor(len/2)]=['w00t'];
65-
benchmark(strict ? notDeepStrictEqual : notDeepEqual,n,values,values2);
84+
benchmark(strict ? notDeepStrictEqual : notDeepEqual,n,values,values2,order);
6685
break;
6786
}
6887
case'notDeepEqual_mixed': {
@@ -71,7 +90,7 @@ function main({ n, len, method, strict }) {
7190
});
7291
constvalues2=values.slice();
7392
values2[0]='w00t';
74-
benchmark(strict ? notDeepStrictEqual : notDeepEqual,n,values,values2);
93+
benchmark(strict ? notDeepStrictEqual : notDeepEqual,n,values,values2,order);
7594
break;
7695
}
7796
default:

‎benchmark/assert/partial-deep-equal.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function createSets(length, extraProps, depth = 0) {
6262
number: i,
6363
},
6464
['array','with','values'],
65-
!depth ? newSet([1,2,{nested: i}]) : newSet(),
65+
!depth ? newSet([1,{nested: i}]) : newSet(),
6666
!depth ? createSets(2,extraProps,depth+1) : null,
6767
]));
6868
}

0 commit comments

Comments
 (0)