Skip to content

Commit 7f85a2c

Browse files
BridgeARtargos
authored andcommitted
assert,util: improve deep equal comparison performance
This is mainly a performance improvement for a lot of simple cases. Diverging elements are detected earlier and equal entries are partially also detected faster. A small correctness patch is also included where recursions now stop as soon as either side has a circular structure. Before, both sides had to have a circular structure at the specific comparison which could have caused more checks that likely fail at a later point. Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de> PR-URL: #46593 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent abba45e commit 7f85a2c

File tree

4 files changed

+175
-132
lines changed

4 files changed

+175
-132
lines changed

benchmark/assert/deepequal-object.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const bench = common.createBenchmark(main, {
1212
combinationFilter: (p) => {
1313
return p.size === 1e4 && p.n === 25 ||
1414
p.size === 1e3 && p.n === 2e2 ||
15-
p.size === 1e2 && p.n === 2e3;
15+
p.size === 1e2 && p.n === 2e3 ||
16+
p.size === 1;
1617
},
1718
});
1819

doc/api/assert.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,10 @@ An alias of [`assert.ok()`][].
546546
<!-- YAML
547547
added: v0.1.21
548548
changes:
549+
- version: REPLACEME
550+
pr-url: https://github.com/nodejs/node/pull/46593
551+
description: Recursion now stops when either side encounters a circular
552+
reference.
549553
- version: v18.0.0
550554
pr-url: https://github.com/nodejs/node/pull/41020
551555
description: Regular expressions lastIndex property is now compared as well.
@@ -617,7 +621,7 @@ are also recursively evaluated by the following rules.
617621
* [Object wrappers][] are compared both as objects and unwrapped values.
618622
* `Object` properties are compared unordered.
619623
* [`Map`][] keys and [`Set`][] items are compared unordered.
620-
* Recursion stops when both sides differ or both sides encounter a circular
624+
* Recursion stops when both sides differ or either side encounters a circular
621625
reference.
622626
* Implementation does not test the [`[[Prototype]]`][prototype-spec] of
623627
objects.
@@ -727,6 +731,10 @@ parameter is an instance of an [`Error`][] then it will be thrown instead of the
727731
<!-- YAML
728732
added: v1.2.0
729733
changes:
734+
- version: REPLACEME
735+
pr-url: https://github.com/nodejs/node/pull/46593
736+
description: Recursion now stops when either side encounters a circular
737+
reference.
730738
- version: v18.0.0
731739
pr-url: https://github.com/nodejs/node/pull/41020
732740
description: Regular expressions lastIndex property is now compared as well.
@@ -780,7 +788,7 @@ are recursively evaluated also by the following rules.
780788
* [Object wrappers][] are compared both as objects and unwrapped values.
781789
* `Object` properties are compared unordered.
782790
* [`Map`][] keys and [`Set`][] items are compared unordered.
783-
* Recursion stops when both sides differ or both sides encounter a circular
791+
* Recursion stops when both sides differ or either side encounters a circular
784792
reference.
785793
* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values. See
786794
below for further details.

0 commit comments

Comments
 (0)