Skip to content

perf(serialize): faster serialization of objects and classes#141

Merged
pi0 merged 9 commits intounjs:mainfrom
zsilbi:perf/opt-string-entries
Mar 25, 2025
Merged

perf(serialize): faster serialization of objects and classes#141
pi0 merged 9 commits intounjs:mainfrom
zsilbi:perf/opt-string-entries

Conversation

@zsilbi
Copy link
Copy Markdown
Collaborator

@zsilbi zsilbi commented Mar 4, 2025

Since we know Object.entries(object) returns Array<[string, any]> we can use a faster path to:

  • avoid calling compare() because we can call String.localeCompare() directly on keys
  • avoid calling serialize() for keys because they can be only strings

I created a new function for handling the Iterable but we can also merge it to the existing serializeObjectEntries() with a flag like onlyStringKeys and add conditions to handle that.

The performance gains are not significant though. - Used a faster path later.

  test/benchmarks.bench.ts > benchmarks > serialize > count:1, size:small 1446ms
     name                      hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · serialize @ main  703,004.23  0.0012  0.4631  0.0014  0.0013  0.0024  0.0026  0.0145  ±0.67%   351503
   · serialize         736,631.27  0.0012  0.2477  0.0014  0.0013  0.0021  0.0022  0.0052  ±0.30%   368316   fastest

  test/benchmarks.bench.ts > benchmarks > serialize > count:256, size:small 1206ms
     name                    hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · serialize @ main  7,052.17  0.1315  0.6230  0.1418  0.1372  0.3075  0.3213  0.3579  ±0.66%     3527
   · serialize         7,302.85  0.1276  0.3518  0.1369  0.1329  0.2274  0.2351  0.3235  ±0.48%     3652   fastest

 BENCH  Summary

  serialize - test/benchmarks.bench.ts > benchmarks > serialize > count:1, size:small
    1.05x faster than serialize @ main

  serialize - test/benchmarks.bench.ts > benchmarks > serialize > count:256, size:small
    1.04x faster than serialize @ main

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.84%. Comparing base (c5173ba) to head (b31c44d).
Report is 94 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main     #141       +/-   ##
===========================================
+ Coverage   80.31%   95.84%   +15.52%     
===========================================
  Files           8        8               
  Lines        1006      481      -525     
  Branches      111      126       +15     
===========================================
- Hits          808      461      -347     
+ Misses        198       18      -180     
- Partials        0        2        +2     

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

🚀 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.

@zsilbi

This comment was marked as outdated.

@zsilbi zsilbi marked this pull request as ready for review March 4, 2025 20:20
@zsilbi zsilbi changed the title perf(serialize): faster serialization of object entries with string keys perf(serialize): faster serialization of objects with string keys Mar 4, 2025
@zsilbi zsilbi requested a review from pi0 March 5, 2025 09:30
@zsilbi

This comment was marked as outdated.

@zsilbi zsilbi changed the title perf(serialize): faster serialization of objects with string keys perf(serialize): faster serialization of objects and classes Mar 5, 2025
@zsilbi zsilbi changed the title perf(serialize): faster serialization of objects and classes perf(serialize): faster serialization of objects and classes and Set Mar 5, 2025
@zsilbi zsilbi changed the title perf(serialize): faster serialization of objects and classes and Set perf(serialize): faster serialization of objects and classes Mar 5, 2025
@zsilbi
Copy link
Copy Markdown
Collaborator Author

zsilbi commented Mar 6, 2025

  test/benchmarks.bench.ts > benchmarks > serialize - presets > count:256, size:small 1216ms
     name                 hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · ohash @ main   7,859.08  0.1117  0.5755  0.1272  0.1195  0.3489  0.3879  0.4571  ±0.99%     3930
   · ohash @ dev   11,888.49  0.0735  0.5062  0.0841  0.0786  0.2925  0.3252  0.3708  ±1.05%     5945   fastest

 BENCH  Summary

  ohash @ dev - test/benchmarks.bench.ts > benchmarks > serialize - presets > count:256, size:small
    1.51x faster than ohash @ main
clk: ~5.25 GHz
cpu: AMD Ryzen 9 7950X 16-Core Processor
runtime: bun 1.2.4 (x64-linux)

benchmark                   avg (min … max) p75 / p99    (min … top 1%)
------------------------------------------- -------------------------------
• count:256, size:small
------------------------------------------- -------------------------------
ohash @ main                 116.81 µs/iter 117.85 µs  
                      (100.96 µs … 1.93 ms) 193.60 µs  
                    (  0.00  b … 528.00 kb)  16.70 kb ██▃▄▆▄▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁

ohash @ dev                   78.58 µs/iter  75.25 µs  
                       (63.59 µs … 1.84 ms) 142.25 µs  
                    (  0.00  b … 792.00 kb)  20.71 kb ▂█▆▂▃▄▂▁▁▁▁▁▁▁▁▂▂▁▁▁▁

summary
  ohash @ dev
   1.49x faster than ohash @ main

@zsilbi zsilbi changed the base branch from main to v3 March 7, 2025 00:10
@zsilbi zsilbi changed the base branch from v3 to main March 7, 2025 00:11
Copy link
Copy Markdown
Member

@pi0 pi0 left a comment

Choose a reason for hiding this comment

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

Thanks!

@pi0 pi0 merged commit f04e052 into unjs:main Mar 25, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants