Skip to content

Conversation

@H4ad
Copy link
Contributor

@H4ad H4ad commented Apr 22, 2023

Instead of concatenating all the strings every time we call toString, just concatenate every time write is called:

hash({}) x 638,508 ops/sec ±1.21% (89 runs sampled)
hash(singleObject) x 94,972 ops/sec ±0.77% (93 runs sampled)
hash(tinyArray) x 10,008 ops/sec ±1.14% (90 runs sampled)
hash(mediumArray) x 961 ops/sec ±1.70% (89 runs sampled)
hash(largeArray) x 61.54 ops/sec ±1.55% (63 runs sampled)
hash(largeJson) x 1.90 ops/sec ±0.87% (9 runs sampled)
objectHash(largeJson, { unorderedObjects: true }) x 1.90 ops/sec ±0.76% (9 runs sampled)

To:

hash({}) x 789,862 ops/sec ±1.21% (88 runs sampled)
hash(singleObject) x 107,655 ops/sec ±1.33% (88 runs sampled)
hash(tinyArray) x 11,503 ops/sec ±0.84% (90 runs sampled)
hash(mediumArray) x 1,146 ops/sec ±1.08% (90 runs sampled)
hash(largeArray) x 69.77 ops/sec ±1.97% (65 runs sampled)
hash(largeJson) x 1.64 ops/sec ±1.69% (9 runs sampled)
objectHash(largeJson, { unorderedObjects: true }) x 1.61 ops/sec ±2.43% (9 runs sampled)

It's good for tiny objects but degrades a little bit for larger objects (for some reason I don't know exactly)

Copy link
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.

Nice improvement 🚀

@codecov
Copy link

codecov bot commented Apr 22, 2023

Codecov Report

Merging #36 (dcbbfed) into main (23431de) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main      #36   +/-   ##
=======================================
  Coverage   80.37%   80.37%           
=======================================
  Files           8        8           
  Lines         968      968           
  Branches      125      125           
=======================================
  Hits          778      778           
  Misses        190      190           
Impacted Files Coverage Δ
src/object-hash.ts 63.11% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@pi0 pi0 merged commit ea2e2cd into unjs:main Apr 22, 2023
@H4ad H4ad deleted the perf/from-array-to-string branch April 22, 2023 10:54
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