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,util: improve array comparison #57619

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

BridgeAR
Copy link
Member

@BridgeAR BridgeAR commented Mar 25, 2025

Sparse arrays and arrays containing undefined are now compared faster using assert.deepStrictEqual() or util.isDeepStrictEqual().

Update: I added another commit that also improves the comparison performance for unequal numbers.

Local benchmark (more configurations and higher iterations to show that even regular arrays improve by ~1%. This should not matter for regular runs, so I kept the benchmark simpler in here):

                                                                                                                         confidence improvement accuracy (*)   (**)  (***)
assert/deepequal-simple-array-and-set.js method='deepEqual_Array' containsUndefined=0 strict=1 len=10000 n=4000                 ***      1.39 %       ±0.67% ±0.90% ±1.17%
assert/deepequal-simple-array-and-set.js method='deepEqual_Array' containsUndefined=1 strict=1 len=10000 n=4000                 ***     24.22 %       ±0.92% ±1.23% ±1.60%
assert/deepequal-simple-array-and-set.js method='deepEqual_Set' containsUndefined=0 strict=1 len=10000 n=4000                            1.41 %       ±1.71% ±2.28% ±2.98%
assert/deepequal-simple-array-and-set.js method='deepEqual_Set' containsUndefined=1 strict=1 len=10000 n=4000                            1.56 %       ±1.57% ±2.08% ±2.71%
assert/deepequal-simple-array-and-set.js method='deepEqual_sparseArray' containsUndefined=0 strict=1 len=10000 n=4000           ***     28.45 %       ±0.80% ±1.06% ±1.38%
assert/deepequal-simple-array-and-set.js method='deepEqual_sparseArray' containsUndefined=1 strict=1 len=10000 n=4000           ***     28.36 %       ±0.85% ±1.14% ±1.48%
assert/deepequal-simple-array-and-set.js method='notDeepEqual_Array' containsUndefined=0 strict=1 len=10000 n=4000                *      1.70 %       ±1.53% ±2.05% ±2.70%
assert/deepequal-simple-array-and-set.js method='notDeepEqual_Array' containsUndefined=1 strict=1 len=10000 n=4000              ***     23.54 %       ±0.95% ±1.26% ±1.64%
assert/deepequal-simple-array-and-set.js method='notDeepEqual_Set' containsUndefined=0 strict=1 len=10000 n=4000                        -1.20 %       ±1.35% ±1.80% ±2.36%
assert/deepequal-simple-array-and-set.js method='notDeepEqual_Set' containsUndefined=1 strict=1 len=10000 n=4000                         0.33 %       ±1.81% ±2.41% ±3.14%
assert/deepequal-simple-array-and-set.js method='notDeepEqual_sparseArray' containsUndefined=0 strict=1 len=10000 n=4000        ***     28.05 %       ±0.86% ±1.15% ±1.49%
assert/deepequal-simple-array-and-set.js method='notDeepEqual_sparseArray' containsUndefined=1 strict=1 len=10000 n=4000        ***     28.11 %       ±0.98% ±1.31% ±1.70%

Sparse arrays and arrays containing undefined are now compared faster
using assert.deepStrictEqual() or util.isDeepStrictEqual().
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/performance

@nodejs-github-bot nodejs-github-bot added assert Issues and PRs related to the assert subsystem. needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module. labels Mar 25, 2025
@BridgeAR BridgeAR added request-ci Add this label to start a Jenkins CI on a PR. needs-benchmark-ci PR that need a benchmark CI run. labels Mar 25, 2025
Copy link

codecov bot commented Mar 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.22%. Comparing base (a4f556f) to head (f46895a).
Report is 9 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #57619      +/-   ##
==========================================
- Coverage   90.24%   90.22%   -0.02%     
==========================================
  Files         630      630              
  Lines      185129   185053      -76     
  Branches    36238    36214      -24     
==========================================
- Hits       167064   166960     -104     
- Misses      11045    11055      +10     
- Partials     7020     7038      +18     
Files with missing lines Coverage Δ
lib/internal/assert/myers_diff.js 89.26% <100.00%> (+0.06%) ⬆️
lib/internal/util/comparisons.js 99.63% <100.00%> (-0.01%) ⬇️

... and 19 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's very surprising to me that checking undefined is faster, especially in the case of getters, but if the benchmarks say it is, so be it :-)

This improves the performance to compare unequal numbers while doing
a deep equal comparison. Comparing for NaN is faster by checking
`variable !== variable` than by using `Number.isNaN()`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assert Issues and PRs related to the assert subsystem. needs-benchmark-ci PR that need a benchmark CI run. needs-ci PRs that need a full CI run. request-ci Add this label to start a Jenkins CI on a PR. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants