Skip to content

Commit 9222fe6

Browse files
BridgeARrefack
authored andcommitted
assert: optimize code path for deepEqual Maps
PR-URL: #14501 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 95bbb68 commit 9222fe6

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

lib/assert.js

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,6 @@ function setEquiv(a, b, strict, memo) {
360360
// This is a lazily initiated Set of entries which have to be compared
361361
// pairwise.
362362
var set = null;
363-
// When the sets contain only value types (eg, lots of numbers), and we're in
364-
// strict mode or if all entries strictly match, we don't need to match the
365-
// entries in a pairwise way. In that case this initialization is done lazily
366-
// to avoid the allocation & bookkeeping cost.
367363
for (const val of a) {
368364
// Note: Checking for the objects first improves the performance for object
369365
// heavy sets but it is a minor slow down for primitives. As they are fast
@@ -384,7 +380,7 @@ function setEquiv(a, b, strict, memo) {
384380

385381
if (set !== null) {
386382
for (const val of b) {
387-
// In non-strict-mode we have to check if a primitive value is already
383+
// We have to check if a primitive value is already
388384
// matching and only if it's not, go hunting for it.
389385
if (typeof val === 'object' && val !== null) {
390386
if (!setHasEqualElement(set, val, strict, memo))
@@ -460,10 +456,13 @@ function mapHasLoosePrim(a, b, key1, memo, item1, item2) {
460456
return false;
461457

462458
for (const val of setA) {
463-
if (!setHasEqualElement(setB, val, false, memo))
459+
if (typeof val === 'object' && val !== null) {
460+
if (!setHasEqualElement(setB, val, false, memo))
461+
return false;
462+
} else if (!setB.has(val) && !setHasLoosePrim(setA, setB, val)) {
464463
return false;
464+
}
465465
}
466-
467466
return true;
468467
}
469468

@@ -483,34 +482,26 @@ function mapHasEqualEntry(set, map, key1, item1, strict, memo) {
483482
}
484483

485484
function mapEquiv(a, b, strict, memo) {
486-
// Caveat: In non-strict mode, this implementation does not handle cases
487-
// where maps contain two equivalent-but-not-reference-equal keys.
488485
if (a.size !== b.size)
489486
return false;
490487

491488
var set = null;
492489

493490
for (const [key, item1] of a) {
494-
// By directly retrieving the value we prevent another b.has(key) check in
495-
// almost all possible cases.
496-
const item2 = b.get(key);
497-
if (item2 === undefined) {
498-
// Just like setEquiv above but in addition we have to make sure the
499-
// values are also equal.
500-
if (typeof key === 'object' && key !== null) {
501-
if (set === null) {
502-
set = new Set();
503-
}
504-
set.add(key);
505-
// Note: we do not have to pass memo in this case as at least one item
506-
// is undefined.
507-
} else if ((!innerDeepEqual(item1, item2, strict) || !b.has(key)) &&
508-
(strict || !mapHasLoosePrim(a, b, key, memo, item1))) {
491+
if (typeof key === 'object' && key !== null) {
492+
if (set === null) {
493+
set = new Set();
494+
}
495+
set.add(key);
496+
} else {
497+
// By directly retrieving the value we prevent another b.has(key) check in
498+
// almost all possible cases.
499+
const item2 = b.get(key);
500+
if ((item2 === undefined && !b.has(key) ||
501+
!innerDeepEqual(item1, item2, strict, memo)) &&
502+
(strict || !mapHasLoosePrim(a, b, key, memo, item1, item2))) {
509503
return false;
510504
}
511-
} else if (!innerDeepEqual(item1, item2, strict, memo) &&
512-
(strict || !mapHasLoosePrim(a, b, key, memo, item1, item2))) {
513-
return false;
514505
}
515506
}
516507

0 commit comments

Comments
 (0)