Skip to content

Commit 00f9503

Browse files
BridgeARrvagg
authored andcommitted
benchmark: improve assert benchmarks
This reduces the runtime and makes sure the strict and loose options can be tested individually. Besides that a couple of redundant cases were removed. PR-URL: #22211 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent dc57d4e commit 00f9503

File tree

10 files changed

+68
-189
lines changed

10 files changed

+68
-189
lines changed

‎benchmark/assert/deepequal-buffer.js‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ const common = require('../common.js');
33
constassert=require('assert');
44

55
constbench=common.createBenchmark(main,{
6-
n: [1e5],
7-
len: [1e2,1e4],
6+
n: [2e4],
7+
len: [1e2,1e3],
8+
strict: [0,1],
89
method: [
910
'deepEqual',
10-
'deepStrictEqual',
11-
'notDeepEqual',
12-
'notDeepStrictEqual'
11+
'notDeepEqual'
1312
]
1413
});
1514

16-
functionmain({ len, n, method }){
15+
functionmain({ len, n, method, strict}){
1716
if(!method)
1817
method='deepEqual';
1918
constdata=Buffer.allocUnsafe(len+1);
@@ -24,6 +23,9 @@ function main({len, n, method }){
2423
data.copy(expected);
2524
data.copy(expectedWrong);
2625

26+
if(strict){
27+
method=method.replace('eep','eepStrict');
28+
}
2729
constfn=assert[method];
2830
constvalue2=method.includes('not') ? expectedWrong : expected;
2931

‎benchmark/assert/deepequal-map.js‎

Lines changed: 9 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,14 @@ const{deepEqual, deepStrictEqual, notDeepEqual, notDeepStrictEqual } =
77
constbench=common.createBenchmark(main,{
88
n: [5e2],
99
len: [5e2],
10+
strict: [0,1],
1011
method: [
1112
'deepEqual_primitiveOnly',
12-
'deepStrictEqual_primitiveOnly',
1313
'deepEqual_objectOnly',
14-
'deepStrictEqual_objectOnly',
1514
'deepEqual_mixed',
16-
'deepStrictEqual_mixed',
17-
'deepEqual_looseMatches',
1815
'notDeepEqual_primitiveOnly',
19-
'notDeepStrictEqual_primitiveOnly',
2016
'notDeepEqual_objectOnly',
21-
'notDeepStrictEqual_objectOnly',
22-
'notDeepEqual_mixed',
23-
'notDeepStrictEqual_mixed',
24-
'notDeepEqual_looseMatches',
17+
'notDeepEqual_mixed'
2518
]
2619
});
2720

@@ -37,7 +30,7 @@ function benchmark(method, n, values, values2){
3730
bench.end(n);
3831
}
3932

40-
functionmain({ n, len, method }){
33+
functionmain({ n, len, method, strict}){
4134
constarray=Array(len).fill(1);
4235
varvalues,values2;
4336

@@ -46,74 +39,33 @@ function main({n, len, method }){
4639
// Empty string falls through to next line as default, mostly for tests.
4740
case'deepEqual_primitiveOnly':
4841
values=array.map((_,i)=>[`str_${i}`,123]);
49-
benchmark(deepEqual,n,values);
50-
break;
51-
case'deepStrictEqual_primitiveOnly':
52-
values=array.map((_,i)=>[`str_${i}`,123]);
53-
benchmark(deepStrictEqual,n,values);
42+
benchmark(strict ? deepStrictEqual : deepEqual,n,values);
5443
break;
5544
case'deepEqual_objectOnly':
5645
values=array.map((_,i)=>[[`str_${i}`,1],123]);
57-
benchmark(deepEqual,n,values);
58-
break;
59-
case'deepStrictEqual_objectOnly':
60-
values=array.map((_,i)=>[[`str_${i}`,1],123]);
61-
benchmark(deepStrictEqual,n,values);
46+
benchmark(strict ? deepStrictEqual : deepEqual,n,values);
6247
break;
6348
case'deepEqual_mixed':
6449
values=array.map((_,i)=>[i%2 ? [`str_${i}`,1] : `str_${i}`,123]);
65-
benchmark(deepEqual,n,values);
66-
break;
67-
case'deepStrictEqual_mixed':
68-
values=array.map((_,i)=>[i%2 ? [`str_${i}`,1] : `str_${i}`,123]);
69-
benchmark(deepStrictEqual,n,values);
70-
break;
71-
case'deepEqual_looseMatches':
72-
values=array.map((_,i)=>[i,i]);
73-
values2=values.slice().map((v)=>[String(v[0]),String(v[1])]);
74-
benchmark(deepEqual,n,values,values2);
50+
benchmark(strict ? deepStrictEqual : deepEqual,n,values);
7551
break;
7652
case'notDeepEqual_primitiveOnly':
7753
values=array.map((_,i)=>[`str_${i}`,123]);
7854
values2=values.slice(0);
7955
values2[Math.floor(len/2)]=['w00t',123];
80-
benchmark(notDeepEqual,n,values,values2);
81-
break;
82-
case'notDeepStrictEqual_primitiveOnly':
83-
values=array.map((_,i)=>[`str_${i}`,123]);
84-
values2=values.slice(0);
85-
values2[Math.floor(len/2)]=['w00t',123];
86-
benchmark(notDeepStrictEqual,n,values,values2);
56+
benchmark(strict ? notDeepStrictEqual : notDeepEqual,n,values,values2);
8757
break;
8858
case'notDeepEqual_objectOnly':
8959
values=array.map((_,i)=>[[`str_${i}`,1],123]);
9060
values2=values.slice(0);
9161
values2[Math.floor(len/2)]=[['w00t'],123];
92-
benchmark(notDeepEqual,n,values,values2);
93-
break;
94-
case'notDeepStrictEqual_objectOnly':
95-
values=array.map((_,i)=>[[`str_${i}`,1],123]);
96-
values2=values.slice(0);
97-
values2[Math.floor(len/2)]=[['w00t'],123];
98-
benchmark(notDeepStrictEqual,n,values,values2);
62+
benchmark(strict ? notDeepStrictEqual : notDeepEqual,n,values,values2);
9963
break;
10064
case'notDeepEqual_mixed':
10165
values=array.map((_,i)=>[i%2 ? [`str_${i}`,1] : `str_${i}`,123]);
10266
values2=values.slice(0);
10367
values2[0]=['w00t',123];
104-
benchmark(notDeepEqual,n,values,values2);
105-
break;
106-
case'notDeepStrictEqual_mixed':
107-
values=array.map((_,i)=>[i%2 ? [`str_${i}`,1] : `str_${i}`,123]);
108-
values2=values.slice(0);
109-
values2[0]=['w00t',123];
110-
benchmark(notDeepStrictEqual,n,values,values2);
111-
break;
112-
case'notDeepEqual_looseMatches':
113-
values=array.map((_,i)=>[i,i]);
114-
values2=values.slice().map((v)=>[String(v[0]),String(v[1])]);
115-
values2[len-1]=[String(len+1),String(len+1)];
116-
benchmark(notDeepEqual,n,values,values2);
68+
benchmark(strict ? notDeepStrictEqual : notDeepEqual,n,values,values2);
11769
break;
11870
default:
11971
thrownewError(`Unsupported method ${method}`);

‎benchmark/assert/deepequal-object.js‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ const common = require('../common.js');
44
constassert=require('assert');
55

66
constbench=common.createBenchmark(main,{
7-
n: [1e6],
8-
size: [1e2,1e3,1e4],
7+
n: [5e3],
8+
size: [1e2,1e3,5e4],
9+
strict: [0,1],
910
method: [
1011
'deepEqual',
11-
'deepStrictEqual',
12-
'notDeepEqual',
13-
'notDeepStrictEqual'
12+
'notDeepEqual'
1413
]
1514
});
1615

@@ -20,14 +19,16 @@ function createObj(source, add = ''){
2019
nope: {
2120
bar: `123${add}`,
2221
a: [1,2,3],
23-
baz: n
22+
baz: n,
23+
c: {},
24+
b: []
2425
}
2526
}));
2627
}
2728

28-
functionmain({ size, n, method }){
29+
functionmain({ size, n, method, strict}){
2930
// TODO: Fix this "hack". `n` should not be manipulated.
30-
n=n/size;
31+
n=Math.min(Math.ceil(n/size),20);
3132

3233
if(!method)
3334
method='deepEqual';
@@ -37,6 +38,9 @@ function main({size, n, method }){
3738
constexpected=createObj(source);
3839
constexpectedWrong=createObj(source,'4');
3940

41+
if(strict){
42+
method=method.replace('eep','eepStrict');
43+
}
4044
constfn=assert[method];
4145
constvalue2=method.includes('not') ? expectedWrong : expected;
4246

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

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,22 @@ const{deepEqual, deepStrictEqual, notDeepEqual, notDeepStrictEqual } =
55
require('assert');
66

77
constprimValues={
8-
'null': null,
9-
'undefined': undefined,
108
'string': 'a',
119
'number': 1,
12-
'boolean': true,
1310
'object': {0: 'a'},
14-
'array': [1,2,3],
15-
'new-array': newArray([1,2,3])
11+
'array': [1,2,3]
1612
};
1713

1814
constbench=common.createBenchmark(main,{
1915
primitive: Object.keys(primValues),
2016
n: [25],
21-
len: [1e5],
17+
len: [2e4],
18+
strict: [0,1],
2219
method: [
2320
'deepEqual_Array',
24-
'deepStrictEqual_Array',
2521
'notDeepEqual_Array',
26-
'notDeepStrictEqual_Array',
2722
'deepEqual_Set',
28-
'deepStrictEqual_Set',
29-
'notDeepEqual_Set',
30-
'notDeepStrictEqual_Set'
23+
'notDeepEqual_Set'
3124
]
3225
});
3326

@@ -39,7 +32,7 @@ function run(fn, n, actual, expected){
3932
bench.end(n);
4033
}
4134

42-
functionmain({ n, len, primitive, method }){
35+
functionmain({ n, len, primitive, method, strict}){
4336
constprim=primValues[primitive];
4437
constactual=[];
4538
constexpected=[];
@@ -62,28 +55,17 @@ function main({n, len, primitive, method }){
6255
// Empty string falls through to next line as default, mostly for tests.
6356
case'':
6457
case'deepEqual_Array':
65-
run(deepEqual,n,actual,expected);
66-
break;
67-
case'deepStrictEqual_Array':
68-
run(deepStrictEqual,n,actual,expected);
58+
run(strict ? deepStrictEqual : deepEqual,n,actual,expected);
6959
break;
7060
case'notDeepEqual_Array':
71-
run(notDeepEqual,n,actual,expectedWrong);
72-
break;
73-
case'notDeepStrictEqual_Array':
74-
run(notDeepStrictEqual,n,actual,expectedWrong);
61+
run(strict ? notDeepStrictEqual : notDeepEqual,n,actual,expectedWrong);
7562
break;
7663
case'deepEqual_Set':
77-
run(deepEqual,n,actualSet,expectedSet);
78-
break;
79-
case'deepStrictEqual_Set':
80-
run(deepStrictEqual,n,actualSet,expectedSet);
64+
run(strict ? deepStrictEqual : deepEqual,n,actualSet,expectedSet);
8165
break;
8266
case'notDeepEqual_Set':
83-
run(notDeepEqual,n,actualSet,expectedWrongSet);
84-
break;
85-
case'notDeepStrictEqual_Set':
86-
run(notDeepStrictEqual,n,actualSet,expectedWrongSet);
67+
run(strict ? notDeepStrictEqual : notDeepEqual,
68+
n,actualSet,expectedWrongSet);
8769
break;
8870
default:
8971
thrownewError(`Unsupported method "${method}"`);

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,33 @@ const common = require('../common.js');
33
constassert=require('assert');
44

55
constprimValues={
6-
'null': null,
7-
'undefined': undefined,
86
'string': 'a',
97
'number': 1,
10-
'boolean': true,
118
'object': {0: 'a'},
12-
'array': [1,2,3],
13-
'new-array': newArray([1,2,3])
9+
'array': [1,2,3]
1410
};
1511

1612
constbench=common.createBenchmark(main,{
1713
primitive: Object.keys(primValues),
18-
n: [1e6],
14+
n: [2e4],
15+
strict: [0,1],
1916
method: [
2017
'deepEqual',
21-
'deepStrictEqual',
2218
'notDeepEqual',
23-
'notDeepStrictEqual'
2419
]
2520
});
2621

27-
functionmain({ n, primitive, method }){
22+
functionmain({ n, primitive, method, strict}){
2823
if(!method)
2924
method='deepEqual';
3025
constprim=primValues[primitive];
3126
constactual=prim;
3227
constexpected=prim;
3328
constexpectedWrong='b';
3429

30+
if(strict){
31+
method=method.replace('eep','eepStrict');
32+
}
3533
constfn=assert[method];
3634
constvalue2=method.includes('not') ? expectedWrong : expected;
3735

0 commit comments

Comments
(0)