Skip to content

Commit 2c974b8

Browse files
BridgeARRafaelGSS
authored andcommitted
benchmark: add assert partialDeepStrictEqual benchmark
The current settings deactivate the extraProps handling, due to the current implementation failing on these cases. PR-URL: #57370 Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
1 parent bc6fbdc commit 2c974b8

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

Diff for: benchmark/assert/partial-deep-equal.js

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const assert = require('assert');
5+
6+
const bench = common.createBenchmark(main, {
7+
n: [25],
8+
size: [500],
9+
extraProps: [0],
10+
datasetName: [
11+
'objects',
12+
'sets',
13+
'maps',
14+
'circularRefs',
15+
'typedArrays',
16+
'arrayBuffers',
17+
'dataViewArrayBuffers',
18+
'array',
19+
],
20+
});
21+
22+
function createArray(length, extraProps) {
23+
if (extraProps) {
24+
return Array.from({ length: length * 4 }, (_, i) => i);
25+
}
26+
return Array.from({ length }, (_, i) => i * 4);
27+
}
28+
29+
function createObjects(length, extraProps, depth = 0) {
30+
return Array.from({ length }, (_, i) => ({
31+
foo: 'yarp',
32+
nope: {
33+
bar: '123',
34+
...extraProps ? { a: [1, 2, i] } : {},
35+
c: {},
36+
b: !depth ? createObjects(2, extraProps, depth + 1) : [],
37+
},
38+
}));
39+
}
40+
41+
function createSets(length, extraProps, depth = 0) {
42+
return Array.from({ length }, (_, i) => new Set([
43+
'yarp',
44+
...extraProps ? ['123', 1, 2] : [],
45+
i + 3,
46+
null,
47+
{
48+
simple: 'object',
49+
number: i,
50+
},
51+
['array', 'with', 'values'],
52+
!depth ? new Set([1, 2, { nested: i }]) : new Set(),
53+
!depth ? createSets(2, extraProps, depth + 1) : null,
54+
]));
55+
}
56+
57+
function createMaps(length, extraProps, depth = 0) {
58+
return Array.from({ length }, (_, i) => new Map([
59+
...extraProps ? [['primitiveKey', 'primitiveValue']] : [],
60+
[42, 'numberKey'],
61+
['objectValue', { a: 1, b: i }],
62+
['arrayValue', [1, 2, i]],
63+
['nestedMap', new Map([['a', i], ['b', { deep: true }]])],
64+
[{ objectKey: true }, 'value from object key'],
65+
[[1, i, 3], 'value from array key'],
66+
[!depth ? createMaps(2, extraProps, depth + 1) : null, 'recursive value' + i],
67+
]));
68+
}
69+
70+
function createCircularRefs(length, extraProps) {
71+
return Array.from({ length }, (_, i) => {
72+
const circularSet = new Set();
73+
const circularMap = new Map();
74+
const circularObj = { name: 'circular object' };
75+
76+
circularSet.add('some value' + i);
77+
circularSet.add(circularSet);
78+
79+
circularMap.set('self', circularMap);
80+
circularMap.set('value', 'regular value');
81+
82+
circularObj.self = circularObj;
83+
84+
const objA = { name: 'A' };
85+
const objB = { name: 'B' };
86+
objA.ref = objB;
87+
objB.ref = objA;
88+
89+
circularSet.add(objA);
90+
circularMap.set('objB', objB);
91+
92+
return {
93+
circularSet,
94+
circularMap,
95+
...extraProps ? { extra: i } : {},
96+
circularObj,
97+
objA,
98+
objB,
99+
};
100+
});
101+
}
102+
103+
function createTypedArrays(length, extraParts) {
104+
const extra = extraParts ? [9, 8, 7] : [];
105+
return Array.from({ length }, (_, i) => {
106+
return {
107+
uint8: new Uint8Array(new ArrayBuffer(32), 4, 4),
108+
int16: new Int16Array([1, 2, ...extra, 3]),
109+
uint32: new Uint32Array([i + 1, i + 2, ...extra, i + 3]),
110+
float64: new Float64Array([1.1, 2.2, ...extra, i + 3.3]),
111+
bigUint64: new BigUint64Array([1n, 2n, 3n]),
112+
};
113+
});
114+
}
115+
116+
function createArrayBuffers(length, extra) {
117+
return Array.from({ length }, (_, n) => new ArrayBuffer(n + extra ? 1 : 0));
118+
}
119+
120+
function createDataViewArrayBuffers(length, extra) {
121+
return Array.from({ length }, (_, n) => new DataView(new ArrayBuffer(n + extra ? 1 : 0)));
122+
}
123+
124+
const datasetMappings = {
125+
objects: createObjects,
126+
sets: createSets,
127+
maps: createMaps,
128+
circularRefs: createCircularRefs,
129+
typedArrays: createTypedArrays,
130+
arrayBuffers: createArrayBuffers,
131+
dataViewArrayBuffers: createDataViewArrayBuffers,
132+
array: createArray,
133+
};
134+
135+
function getDatasets(datasetName, size, extra) {
136+
return {
137+
actual: datasetMappings[datasetName](size, true),
138+
expected: datasetMappings[datasetName](size, !extra),
139+
};
140+
}
141+
142+
function main({ size, n, datasetName, extraProps }) {
143+
const { actual, expected } = getDatasets(datasetName, size, extraProps);
144+
145+
bench.start();
146+
for (let i = 0; i < n; ++i) {
147+
assert.partialDeepStrictEqual(actual, expected);
148+
}
149+
bench.end(n);
150+
}

0 commit comments

Comments
 (0)