Skip to content

Commit 989b529

Browse files
committed
assert: improve partialDeepStrictEqual performance and add benchmark
1 parent 6b3937a commit 989b529

File tree

2 files changed

+142
-22
lines changed

2 files changed

+142
-22
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const assert = require('assert');
5+
6+
const bench = common.createBenchmark(main, {
7+
n: [10, 50, 200],
8+
size: [1e3],
9+
datasetName: ['objects', 'simpleSets', 'complexSets', 'maps', 'arrayBuffers', 'dataViewArrayBuffers'],
10+
});
11+
12+
function createObjects(length, depth = 0) {
13+
return Array.from({ length }, () => ({
14+
foo: 'yarp',
15+
nope: {
16+
bar: '123',
17+
a: [1, 2, 3],
18+
c: {},
19+
b: !depth ? createObjects(2, depth + 1) : [],
20+
},
21+
}));
22+
}
23+
24+
function createSimpleSets(length, depth = 0) {
25+
return Array.from({ length }, () => new Set([
26+
'yarp',
27+
'123',
28+
1,
29+
2,
30+
3,
31+
null,
32+
]));
33+
}
34+
35+
function createComplexSets(length, depth = 0) {
36+
return Array.from({ length }, () => new Set([
37+
'yarp',
38+
{
39+
bar: '123',
40+
a: [1, 2, 3],
41+
c: {},
42+
b: !depth ? createComplexSets(2, depth + 1) : new Set(),
43+
},
44+
]));
45+
}
46+
47+
function createMaps(length, depth = 0) {
48+
return Array.from({ length }, () => new Map([
49+
['foo', 'yarp'],
50+
['nope', new Map([
51+
['bar', '123'],
52+
['a', [1, 2, 3]],
53+
['c', {}],
54+
['b', !depth ? createMaps(2, depth + 1) : new Map()],
55+
])],
56+
]));
57+
}
58+
59+
function createArrayBuffers(length) {
60+
return Array.from({ length }, (_, n) => {
61+
return new ArrayBuffer(n);
62+
});
63+
}
64+
65+
function createDataViewArrayBuffers(length) {
66+
return Array.from({ length }, (_, n) => {
67+
return new DataView(new ArrayBuffer(n));
68+
});
69+
}
70+
71+
const datasetMappings = {
72+
objects: createObjects,
73+
simpleSets: createSimpleSets,
74+
complexSets: createComplexSets,
75+
maps: createMaps,
76+
arrayBuffers: createArrayBuffers,
77+
dataViewArrayBuffers: createDataViewArrayBuffers,
78+
};
79+
80+
function getDatasets(datasetName, size) {
81+
return {
82+
actual: datasetMappings[datasetName](size),
83+
expected: datasetMappings[datasetName](size),
84+
};
85+
}
86+
87+
function main({ size, n, datasetName }) {
88+
const { actual, expected } = getDatasets(datasetName, size);
89+
90+
bench.start();
91+
for (let i = 0; i < n; ++i) {
92+
assert.partialDeepStrictEqual(actual, expected);
93+
}
94+
bench.end(n);
95+
}

lib/assert.js

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
const {
2424
ArrayBufferIsView,
2525
ArrayBufferPrototypeGetByteLength,
26-
ArrayFrom,
2726
ArrayIsArray,
2827
ArrayPrototypeIndexOf,
2928
ArrayPrototypeJoin,
@@ -395,12 +394,11 @@ function partiallyCompareMaps(actual, expected, comparedObjects) {
395394
const expectedIterator = FunctionPrototypeCall(SafeMap.prototype[SymbolIterator], expected);
396395

397396
for (const { 0: key, 1: expectedValue } of expectedIterator) {
398-
if (!MapPrototypeHas(actual, key)) {
397+
const actualValue = MapPrototypeGet(actual, key);
398+
if (actualValue === undefined && !MapPrototypeHas(actual, key)) {
399399
return false;
400400
}
401401

402-
const actualValue = MapPrototypeGet(actual, key);
403-
404402
if (!compareBranch(actualValue, expectedValue, comparedObjects)) {
405403
return false;
406404
}
@@ -476,23 +474,39 @@ function partiallyCompareArrayBuffersOrViews(actual, expected) {
476474

477475
function partiallyCompareSets(actual, expected, comparedObjects) {
478476
if (SetPrototypeGetSize(expected) > SetPrototypeGetSize(actual)) {
479-
return false; // `expected` can't be a subset if it has more elements
477+
return false;
480478
}
481479

482480
if (isDeepEqual === undefined) lazyLoadComparison();
483481

484-
const actualArray = ArrayFrom(FunctionPrototypeCall(SafeSet.prototype[SymbolIterator], actual));
482+
const actualSet = new SafeSet();
483+
for (const item of FunctionPrototypeCall(SafeSet.prototype[SymbolIterator], actual)) {
484+
if (!isPrimitive(item)) {
485+
actualSet.add(item);
486+
}
487+
}
488+
485489
const expectedIterator = FunctionPrototypeCall(SafeSet.prototype[SymbolIterator], expected);
486-
const usedIndices = new SafeSet();
487490

488-
expectedIteration: for (const expectedItem of expectedIterator) {
489-
for (let actualIdx = 0; actualIdx < actualArray.length; actualIdx++) {
490-
if (!usedIndices.has(actualIdx) && isDeepStrictEqual(actualArray[actualIdx], expectedItem)) {
491-
usedIndices.add(actualIdx);
492-
continue expectedIteration;
491+
for (const expectedItem of expectedIterator) {
492+
let foundMatch = false;
493+
494+
// Check for direct inclusion first (fast path for both primitives and identical object references)
495+
if (actual.has(expectedItem)) {
496+
foundMatch = true;
497+
} else if (!isPrimitive(expectedItem)) {
498+
for (const actualItem of actualSet) {
499+
if (isDeepStrictEqual(actualItem, expectedItem)) {
500+
actualSet.delete(actualItem);
501+
foundMatch = true;
502+
break;
503+
}
493504
}
494505
}
495-
return false;
506+
507+
if (!foundMatch) {
508+
return false;
509+
}
496510
}
497511

498512
return true;
@@ -510,21 +524,26 @@ function getZeroKey(item) {
510524
}
511525

512526
function partiallyCompareArrays(actual, expected, comparedObjects) {
527+
if (actual === expected) return true;
528+
513529
if (expected.length > actual.length) {
514530
return false;
515531
}
516532

533+
if (expected.length === 0) {
534+
return true;
535+
}
536+
517537
if (isDeepEqual === undefined) lazyLoadComparison();
518538

519539
// Create a map to count occurrences of each element in the expected array
520540
const expectedCounts = new SafeMap();
521-
const safeExpected = new SafeArrayIterator(expected);
522541

523-
for (const expectedItem of safeExpected) {
524-
// Check if the item is a zero or a -0, as these need to be handled separately
542+
const expectedIterator = new SafeArrayIterator(expected);
543+
for (const expectedItem of expectedIterator) {
525544
if (expectedItem === 0) {
526545
const zeroKey = getZeroKey(expectedItem);
527-
expectedCounts.set(zeroKey, (expectedCounts.get(zeroKey)?.count || 0) + 1);
546+
expectedCounts.set(zeroKey, (expectedCounts.get(zeroKey) ?? 0) + 1);
528547
} else {
529548
let found = false;
530549
for (const { 0: key, 1: count } of expectedCounts) {
@@ -540,10 +559,8 @@ function partiallyCompareArrays(actual, expected, comparedObjects) {
540559
}
541560
}
542561

543-
const safeActual = new SafeArrayIterator(actual);
544-
545-
for (const actualItem of safeActual) {
546-
// Check if the item is a zero or a -0, as these need to be handled separately
562+
const actualIterator = new SafeArrayIterator(actual);
563+
for (const actualItem of actualIterator) {
547564
if (actualItem === 0) {
548565
const zeroKey = getZeroKey(actualItem);
549566

@@ -567,6 +584,10 @@ function partiallyCompareArrays(actual, expected, comparedObjects) {
567584
}
568585
}
569586
}
587+
588+
if (expectedCounts.size === 0) {
589+
return true;
590+
}
570591
}
571592

572593
return expectedCounts.size === 0;
@@ -723,6 +744,10 @@ function compareExceptionKey(actual, expected, key, message, keys, fn) {
723744
}
724745
}
725746

747+
function isPrimitive(value) {
748+
return typeof value !== 'object' || value === null;
749+
}
750+
726751
function expectedException(actual, expected, message, fn) {
727752
let generatedMessage = false;
728753
let throwError = false;
@@ -741,7 +766,7 @@ function expectedException(actual, expected, message, fn) {
741766
}
742767
throwError = true;
743768
// Handle primitives properly.
744-
} else if (typeof actual !== 'object' || actual === null) {
769+
} else if (isPrimitive(actual)) {
745770
const err = new AssertionError({
746771
actual,
747772
expected,

0 commit comments

Comments
 (0)