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

feat(benchmark): add defu to compare with #486

Merged
merged 4 commits into from
Jul 20, 2024

Conversation

xeho91
Copy link
Contributor

@xeho91 xeho91 commented Jul 20, 2024

Hi @RebeccaStevens 👋!

I hope I am not bringing a bad news with this PR, nor any negative feelings!

I've created a PR on Storybook, where I am trying to replace lodash/merge and lodash/mergeWith with an alternative.
Initially I thought of your package, because it was the only one I knew so far.

Until I learned about defu.

Naturally, I had answer one of question - performance. You have done an outstanding work on this package, because you have benchmarks, where it was easy to add defu to compare with.

It looks like defu wins with a big margin, but I am giving a benefit of the doubt to know if my benchmark setup is wrong, or I am missing something.

Results

Device: MacBook Pro M2

Results during invalid usage - before fix
No benchmark data file found. Generating random data for benchmarking against.

Running benchmarks for data set "tall" (1 of 3):

┌─────────┬──────────────────────┬─────────────┬────────────────────┬──────────┬──────────┐
│ (index) │ Task Name            │ ops/sec     │ Average Time (ns)  │ Margin   │ Samples  │
├─────────┼──────────────────────┼─────────────┼────────────────────┼──────────┼──────────┤
│ 0       │ 'deepmerge-ts'       │ '527,615'   │ 1895.3208846141354 │ '±0.19%' │ 5276152  │
│ 1       │ 'deepmerge'          │ '3'         │ 289569472.22222215 │ '±4.69%' │ 36       │
│ 2       │ 'defu'               │ '6,208,484' │ 161.06991183128463 │ '±0.05%' │ 62084842 │
│ 3       │ 'merge-anything'     │ '664,669'   │ 1504.507973736976  │ '±0.52%' │ 6646692  │
│ 4       │ 'object-accumulator' │ '9'         │ 106072233.74736802 │ '±4.88%' │ 95       │
│ 5       │ 'lodash merge'       │ '4'         │ 205049445.46938705 │ '±1.56%' │ 49       │
└─────────┴──────────────────────┴─────────────┴────────────────────┴──────────┴──────────┘

Running benchmarks for data set "wide" (2 of 3):

┌─────────┬──────────────────────┬─────────────┬────────────────────┬───────────┬──────────┐
│ (index) │ Task Name            │ ops/sec     │ Average Time (ns)  │ Margin    │ Samples  │
├─────────┼──────────────────────┼─────────────┼────────────────────┼───────────┼──────────┤
│ 0       │ 'deepmerge-ts'       │ '2'         │ 425422401.000001   │ '±2.76%'  │ 24       │
│ 1       │ 'deepmerge'          │ '0'         │ 9545288958.50001   │ '±55.55%' │ 2        │
│ 2       │ 'defu'               │ '6,210,981' │ 161.00515641617744 │ '±0.26%'  │ 62109813 │
│ 3       │ 'merge-anything'     │ '1'         │ 921586829.5454526  │ '±0.23%'  │ 11       │
│ 4       │ 'object-accumulator' │ '177'       │ 5641003.027636446  │ '±1.22%'  │ 1773     │
│ 5       │ 'lodash merge'       │ '1'         │ 648883515.5000015  │ '±1.00%'  │ 16       │
└─────────┴──────────────────────┴─────────────┴────────────────────┴───────────┴──────────┘

Running benchmarks for data set "mid" (3 of 3):

┌─────────┬──────────────────────┬─────────────┬────────────────────┬──────────┬──────────┐
│ (index) │ Task Name            │ ops/sec     │ Average Time (ns)  │ Margin   │ Samples  │
├─────────┼──────────────────────┼─────────────┼────────────────────┼──────────┼──────────┤
│ 0       │ 'deepmerge-ts'       │ '23'        │ 41874533.28451813  │ '±0.49%' │ 239      │
│ 1       │ 'deepmerge'          │ '0'         │ 2443575399.9999957 │ '±2.27%' │ 5        │
│ 2       │ 'defu'               │ '5,881,482' │ 170.02515962682915 │ '±0.66%' │ 58814826 │
│ 3       │ 'merge-anything'     │ '28'        │ 35619074.024909645 │ '±0.42%' │ 281      │
│ 4       │ 'object-accumulator' │ '150'       │ 6660438.356858103  │ '±1.70%' │ 1502     │
│ 5       │ 'lodash merge'       │ '2'         │ 377150435.29629904 │ '±0.40%' │ 27       │
└─────────┴──────────────────────┴─────────────┴────────────────────┴──────────┴──────────┘
Results after the possible fix
Loading benchmark data file.

Running benchmarks for data set "tall" (1 of 3):

┌─────────┬──────────────────────┬─────────────┬────────────────────┬──────────┬──────────┐
│ (index) │ Task Name            │ ops/sec     │ Average Time (ns)  │ Margin   │ Samples  │
├─────────┼──────────────────────┼─────────────┼────────────────────┼──────────┼──────────┤
│ 0       │ 'deepmerge-ts'       │ '517,389'   │ 1932.7816774650069 │ '±0.28%' │ 5173891  │
│ 1       │ 'deepmerge'          │ '3'         │ 283752828.8611113  │ '±3.94%' │ 36       │
│ 2       │ 'defu'               │ '1,116,263' │ 895.8455933085199  │ '±0.27%' │ 11162638 │
│ 3       │ 'merge-anything'     │ '690,933'   │ 1447.316283031864  │ '±0.26%' │ 6909340  │
│ 4       │ 'object-accumulator' │ '9'         │ 101954385.53535335 │ '±1.49%' │ 99       │
│ 5       │ 'lodash merge'       │ '5'         │ 192098338.07547063 │ '±0.39%' │ 53       │
└─────────┴──────────────────────┴─────────────┴────────────────────┴──────────┴──────────┘

Running benchmarks for data set "wide" (2 of 3):

┌─────────┬──────────────────────┬─────────┬────────────────────┬───────────┬─────────┐
│ (index) │ Task Name            │ ops/sec │ Average Time (ns)  │ Margin    │ Samples │
├─────────┼──────────────────────┼─────────┼────────────────────┼───────────┼─────────┤
│ 0       │ 'deepmerge-ts'       │ '2'     │ 403044879.9999984  │ '±0.09%'  │ 25      │
│ 1       │ 'deepmerge'          │ '0'     │ 10142960707.999985 │ '±0.00%'  │ 1       │
│ 2       │ 'defu'               │ '2'     │ 412902348.2400016  │ '±3.19%'  │ 25      │
│ 3       │ 'merge-anything'     │ '1'     │ 951229765.0909168  │ '±11.77%' │ 11      │
│ 4       │ 'object-accumulator' │ '177'   │ 5644815.621895985  │ '±1.92%'  │ 1772    │
│ 5       │ 'lodash merge'       │ '1'     │ 618995225.4117645  │ '±2.30%'  │ 17      │
└─────────┴──────────────────────┴─────────┴────────────────────┴───────────┴─────────┘

Running benchmarks for data set "mid" (3 of 3):

┌─────────┬──────────────────────┬─────────┬────────────────────┬──────────┬─────────┐
│ (index) │ Task Name            │ ops/sec │ Average Time (ns)  │ Margin   │ Samples │
├─────────┼──────────────────────┼─────────┼────────────────────┼──────────┼─────────┤
│ 0       │ 'deepmerge-ts'       │ '23'    │ 42636126.81276876  │ '±1.98%' │ 235     │
│ 1       │ 'deepmerge'          │ '0'     │ 2439054750.0000043 │ '±1.43%' │ 5       │
│ 2       │ 'defu'               │ '64'    │ 15539177.942545502 │ '±0.45%' │ 644     │
│ 3       │ 'merge-anything'     │ '26'    │ 37383370.03731453  │ '±2.42%' │ 268     │
│ 4       │ 'object-accumulator' │ '154'   │ 6478492.177460992  │ '±0.12%' │ 1544    │
│ 5       │ 'lodash merge'       │ '2'     │ 386707298.0384613  │ '±3.00%' │ 26      │
└─────────┴──────────────────────┴─────────┴────────────────────┴──────────┴─────────┘

While Storybook has just a simple need for merge. I still want to give your package a chance. Do you think there's any other reason you believe they should favour your package over defu?

@xeho91
Copy link
Contributor Author

xeho91 commented Jul 20, 2024

I might have used defu incorrectly. After the fix, the new benchmark results are different and feels more realistic.

Loading benchmark data file.

Running benchmarks for data set "tall" (1 of 3):

┌─────────┬──────────────────────┬─────────────┬────────────────────┬──────────┬──────────┐
│ (index) │ Task Name            │ ops/sec     │ Average Time (ns)  │ Margin   │ Samples  │
├─────────┼──────────────────────┼─────────────┼────────────────────┼──────────┼──────────┤
│ 0       │ 'deepmerge-ts'       │ '517,389'   │ 1932.7816774650069 │ '±0.28%' │ 5173891  │
│ 1       │ 'deepmerge'          │ '3'         │ 283752828.8611113  │ '±3.94%' │ 36       │
│ 2       │ 'defu'               │ '1,116,263' │ 895.8455933085199  │ '±0.27%' │ 11162638 │
│ 3       │ 'merge-anything'     │ '690,933'   │ 1447.316283031864  │ '±0.26%' │ 6909340  │
│ 4       │ 'object-accumulator' │ '9'         │ 101954385.53535335 │ '±1.49%' │ 99       │
│ 5       │ 'lodash merge'       │ '5'         │ 192098338.07547063 │ '±0.39%' │ 53       │
└─────────┴──────────────────────┴─────────────┴────────────────────┴──────────┴──────────┘

Running benchmarks for data set "wide" (2 of 3):

┌─────────┬──────────────────────┬─────────┬────────────────────┬───────────┬─────────┐
│ (index) │ Task Name            │ ops/sec │ Average Time (ns)  │ Margin    │ Samples │
├─────────┼──────────────────────┼─────────┼────────────────────┼───────────┼─────────┤
│ 0       │ 'deepmerge-ts'       │ '2'     │ 403044879.9999984  │ '±0.09%'  │ 25      │
│ 1       │ 'deepmerge'          │ '0'     │ 10142960707.999985 │ '±0.00%'  │ 1       │
│ 2       │ 'defu'               │ '2'     │ 412902348.2400016  │ '±3.19%'  │ 25      │
│ 3       │ 'merge-anything'     │ '1'     │ 951229765.0909168  │ '±11.77%' │ 11      │
│ 4       │ 'object-accumulator' │ '177'   │ 5644815.621895985  │ '±1.92%'  │ 1772    │
│ 5       │ 'lodash merge'       │ '1'     │ 618995225.4117645  │ '±2.30%'  │ 17      │
└─────────┴──────────────────────┴─────────┴────────────────────┴───────────┴─────────┘

Running benchmarks for data set "mid" (3 of 3):

┌─────────┬──────────────────────┬─────────┬────────────────────┬──────────┬─────────┐
│ (index) │ Task Name            │ ops/sec │ Average Time (ns)  │ Margin   │ Samples │
├─────────┼──────────────────────┼─────────┼────────────────────┼──────────┼─────────┤
│ 0       │ 'deepmerge-ts'       │ '23'    │ 42636126.81276876  │ '±1.98%' │ 235     │
│ 1       │ 'deepmerge'          │ '0'     │ 2439054750.0000043 │ '±1.43%' │ 5       │
│ 2       │ 'defu'               │ '64'    │ 15539177.942545502 │ '±0.45%' │ 644     │
│ 3       │ 'merge-anything'     │ '26'    │ 37383370.03731453  │ '±2.42%' │ 268     │
│ 4       │ 'object-accumulator' │ '154'   │ 6478492.177460992  │ '±0.12%' │ 1544    │
│ 5       │ 'lodash merge'       │ '2'     │ 386707298.0384613  │ '±3.00%' │ 26      │
└─────────┴──────────────────────┴─────────┴────────────────────┴──────────┴─────────┘

Copy link

codecov bot commented Jul 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.37%. Comparing base (4951acd) to head (aebf8d9).
Report is 45 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #486      +/-   ##
==========================================
- Coverage   99.50%   99.37%   -0.14%     
==========================================
  Files           8        8              
  Lines        1224     1276      +52     
  Branches      111      119       +8     
==========================================
+ Hits         1218     1268      +50     
- Misses          6        8       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@RebeccaStevens
Copy link
Owner

Hey, I wasn't aware of that project but I'm happy to add it to the benchmarks.

I had a quick look at the project and I would indeed expect it to be quite a bit faster than this one; as it's much more minimal.

Performance is definitely important to this project but we're quite feature-rich compared to other similar projects. This prevents us from being the fastest as we need to do a few more checks during the merge process. If performance is most important to you, we're probably not the best choice (the best option in that case would probably be to roll your own merging function optimized for your needs).

@RebeccaStevens RebeccaStevens merged commit 93599b4 into RebeccaStevens:main Jul 20, 2024
15 checks passed
github-actions bot pushed a commit that referenced this pull request Jul 20, 2024
# [7.1.0](v7.0.3...v7.1.0) (2024-07-20)

### Features

* **benchmark:** add `defu` to compare with ([#486](#486)) ([93599b4](93599b4))
Copy link

🎉 This PR is included in version 7.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions github-actions bot added the Status: Released It's now live. label Jul 20, 2024
@xeho91 xeho91 deleted the benchmark-defu branch July 20, 2024 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Released It's now live.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants