Skip to content
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

assert: improve partialDeepStrictEqual #57370

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion benchmark/assert/deepequal-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { deepEqual, deepStrictEqual, notDeepEqual, notDeepStrictEqual } =
require('assert');

const bench = common.createBenchmark(main, {
n: [5e3],
n: [2e3],
len: [5e2],
strict: [0, 1],
method: [
Expand Down
4 changes: 2 additions & 2 deletions benchmark/assert/deepequal-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [25, 2e2],
n: [50, 2e2],
size: [1e2, 1e4],
method: ['deepEqual', 'notDeepEqual', 'deepStrictEqual', 'notDeepStrictEqual'],
}, {
combinationFilter: (p) => {
return p.size === 1e4 && p.n === 25 ||
return p.size === 1e4 && p.n === 50 ||
p.size === 1e3 && p.n === 2e2 ||
p.size === 1e2 && p.n === 2e3 ||
p.size === 1;
Expand Down
2 changes: 1 addition & 1 deletion benchmark/assert/deepequal-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { deepEqual, deepStrictEqual, notDeepEqual, notDeepStrictEqual } =
require('assert');

const bench = common.createBenchmark(main, {
n: [5e2],
n: [1e3],
len: [5e2],
strict: [0, 1],
method: [
Expand Down
2 changes: 1 addition & 1 deletion benchmark/assert/deepequal-simple-array-and-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { deepEqual, deepStrictEqual, notDeepEqual, notDeepStrictEqual } =
require('assert');

const bench = common.createBenchmark(main, {
n: [5e2],
n: [1e3],
len: [1e4],
strict: [1],
method: [
Expand Down
6 changes: 6 additions & 0 deletions benchmark/assert/deepequal-typedarrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const bench = common.createBenchmark(main, {
'notDeepEqual',
],
len: [1e2, 5e3],
}, {
combinationFilter(p) {
return p.strict === 1 ||
p.type !== 'Float32Array' ||
p.len === 1e2;
},
});

function main({ type, n, len, method, strict }) {
Expand Down
9 changes: 8 additions & 1 deletion benchmark/assert/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [25, 2e7],
n: [2e7],
method: ['match', 'doesNotMatch'],
}, {
combinationFilter(p) {
// These benchmarks purposefully do not run by default. They do not provide
// might insight, due to only being a small wrapper around a native regexp
// call.
return p.n === 1;
},
});

function main({ n, method }) {
Expand Down
150 changes: 150 additions & 0 deletions benchmark/assert/partial-deep-equal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
'use strict';

const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [25],
size: [500],
extraProps: [0],
datasetName: [
'objects',
'sets',
'maps',
'circularRefs',
'typedArrays',
'arrayBuffers',
'dataViewArrayBuffers',
'array',
],
});

function createArray(length, extraProps) {
if (extraProps) {
return Array.from({ length: length * 4 }, (_, i) => i);
}
return Array.from({ length }, (_, i) => i * 4);
}

function createObjects(length, extraProps, depth = 0) {
return Array.from({ length }, (_, i) => ({
foo: 'yarp',
nope: {
bar: '123',
...extraProps ? { a: [1, 2, i] } : {},
c: {},
b: !depth ? createObjects(2, extraProps, depth + 1) : [],
},
}));
}

function createSets(length, extraProps, depth = 0) {
return Array.from({ length }, (_, i) => new Set([
'yarp',
...extraProps ? ['123', 1, 2] : [],
i + 3,
null,
{
simple: 'object',
number: i,
},
['array', 'with', 'values'],
!depth ? new Set([1, 2, { nested: i }]) : new Set(),
!depth ? createSets(2, extraProps, depth + 1) : null,
]));
}

function createMaps(length, extraProps, depth = 0) {
return Array.from({ length }, (_, i) => new Map([
...extraProps ? [['primitiveKey', 'primitiveValue']] : [],
[42, 'numberKey'],
['objectValue', { a: 1, b: i }],
['arrayValue', [1, 2, i]],
['nestedMap', new Map([['a', i], ['b', { deep: true }]])],
[{ objectKey: true }, 'value from object key'],
[[1, i, 3], 'value from array key'],
[!depth ? createMaps(2, extraProps, depth + 1) : null, 'recursive value' + i],
]));
}

function createCircularRefs(length, extraProps) {
return Array.from({ length }, (_, i) => {
const circularSet = new Set();
const circularMap = new Map();
const circularObj = { name: 'circular object' };

circularSet.add('some value' + i);
circularSet.add(circularSet);

circularMap.set('self', circularMap);
circularMap.set('value', 'regular value');

circularObj.self = circularObj;

const objA = { name: 'A' };
const objB = { name: 'B' };
objA.ref = objB;
objB.ref = objA;

circularSet.add(objA);
circularMap.set('objB', objB);

return {
circularSet,
circularMap,
...extraProps ? { extra: i } : {},
circularObj,
objA,
objB,
};
});
}

function createTypedArrays(length, extraParts) {
const extra = extraParts ? [9, 8, 7] : [];
return Array.from({ length }, (_, i) => {
return {
uint8: new Uint8Array(new ArrayBuffer(32), 4, 4),
int16: new Int16Array([1, 2, ...extra, 3]),
uint32: new Uint32Array([i + 1, i + 2, ...extra, i + 3]),
float64: new Float64Array([1.1, 2.2, ...extra, i + 3.3]),
bigUint64: new BigUint64Array([1n, 2n, 3n]),
};
});
}

function createArrayBuffers(length, extra) {
return Array.from({ length }, (_, n) => new ArrayBuffer(n + extra ? 1 : 0));
}

function createDataViewArrayBuffers(length, extra) {
return Array.from({ length }, (_, n) => new DataView(new ArrayBuffer(n + extra ? 1 : 0)));
}

const datasetMappings = {
objects: createObjects,
sets: createSets,
maps: createMaps,
circularRefs: createCircularRefs,
typedArrays: createTypedArrays,
arrayBuffers: createArrayBuffers,
dataViewArrayBuffers: createDataViewArrayBuffers,
array: createArray,
};

function getDatasets(datasetName, size, extra) {
return {
actual: datasetMappings[datasetName](size, true),
expected: datasetMappings[datasetName](size, !extra),
};
}

function main({ size, n, datasetName, extraProps }) {
const { actual, expected } = getDatasets(datasetName, size, extraProps);

bench.start();
for (let i = 0; i < n; ++i) {
assert.partialDeepStrictEqual(actual, expected);
}
bench.end(n);
}
9 changes: 8 additions & 1 deletion benchmark/assert/rejects.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [25, 2e5],
n: [2e5],
method: ['rejects', 'doesNotReject'],
}, {
combinationFilter(p) {
// These benchmarks purposefully do not run by default. They do not provide
// much insight, due to only being a small wrapper around a native promise
// with a few extra checks.
return p.n === 1;
},
});

async function main({ n, method }) {
Expand Down
8 changes: 7 additions & 1 deletion benchmark/assert/strictequal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [25, 2e5],
n: [2e5],
type: ['string', 'object', 'number'],
method: ['strictEqual', 'notStrictEqual'],
}, {
combinationFilter(p) {
// These benchmarks purposefully do not run by default. They do not provide
// much insight, due to only being a small wrapper around `Object.is()`.
return p.n === 1;
},
});

function main({ type, n, method }) {
Expand Down
8 changes: 7 additions & 1 deletion benchmark/assert/throws.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [25, 2e5],
n: [2e5],
method: ['throws', 'doesNotThrow'],
}, {
combinationFilter(p) {
// These benchmarks purposefully do not run by default. They do not provide
// much insight, due to only being a small wrapper around a try / catch.
return p.n === 1;
},
});

function main({ n, method }) {
Expand Down
Loading
Loading