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

perf(object-hash): faster circular checking by using map #34

Merged
merged 1 commit into from Apr 22, 2023

Conversation

H4ad
Copy link
Contributor

@H4ad H4ad commented Apr 22, 2023

Inspired by puleos/object-hash#122

perf: faster circular checking by using map

This one is good specially for big objects, using Map instead using Array this goes from:

hash({}) x 629,807 ops/sec ±1.22% (88 runs sampled)
hash(singleObject) x 93,377 ops/sec ±2.32% (90 runs sampled)
hash(tinyArray) x 10,114 ops/sec ±1.23% (88 runs sampled)
hash(mediumArray) x 981 ops/sec ±1.32% (87 runs sampled)
hash(largeArray) x 61.76 ops/sec ±1.70% (63 runs sampled)
hash(largeJson) x 1.90 ops/sec ±0.93% (9 runs sampled)
objectHash(largeJson, { unorderedObjects: true }) x 1.89 ops/sec ±1.15% (9 runs sampled)

To:

hash({}) x 667,828 ops/sec ±6.24% (78 runs sampled)
hash(singleObject) x 89,026 ops/sec ±1.10% (89 runs sampled)
hash(tinyArray) x 9,742 ops/sec ±1.09% (90 runs sampled)
hash(mediumArray) x 969 ops/sec ±1.19% (89 runs sampled)
hash(largeArray) x 79.65 ops/sec ±1.22% (64 runs sampled)
hash(largeJson) x 3.91 ops/sec ±1.73% (14 runs sampled)
objectHash(largeJson, { unorderedObjects: true }) x 3.91 ops/sec ±1.96% (14 runs sampled)
benchmark.js

Unzip to use with the benchmark:

const Benchmark = require('benchmark');
const { objectHash } = require('../dist/index.cjs');
const largeJson = require('./large.json');

function generateItems(num) {
  return new Array(num).fill(0).map(() => {
    return {
      propNum: Math.random(),
      propBool: Math.random() > 0.5,
      propString: Math.random().toString(16),
      propDate: new Date(),
      propObj: {
        propNum: Math.random(),
        propBool: Math.random() > 0.5,
        propString: Math.random().toString(16),
        propDate: new Date(),
      }
    }
  })
}

const suite = new Benchmark.Suite();
const singleObject = generateItems(1)[0];
const tinyArray = generateItems(10);
const mediumArray = generateItems(100);
const largeArray = generateItems(1000);


suite.add('hash({})', function () {
  const v = objectHash({});
});

suite.add('hash(singleObject)', function () {
  const v = objectHash(singleObject);
});

suite.add('hash(tinyArray)', function () {
  const v = objectHash(tinyArray);
});

suite.add('hash(mediumArray)', function () {
  const v = objectHash(mediumArray);
});

suite.add('hash(largeArray)', function () {
  const v = objectHash(largeArray);
});

suite.add('hash(largeJson)', function () {
  const v = objectHash(largeJson);
});

suite.add('objectHash(largeJson, { unorderedObjects: true })', function () {
  const v = objectHash(largeJson, { unorderedObjects: true });
});

suite
  // add listeners
  .on('cycle', function (event) {
    console.log(event.target.toString());
  })
  .on('complete', function () {
    console.log('Fastest is ' + this.filter('fastest').map('name'));
  })
  .run({
    async: false,
  });

@codecov
Copy link

codecov bot commented Apr 22, 2023

Codecov Report

Merging #34 (e31ef33) into main (2bebe58) will decrease coverage by 0.08%.
The diff coverage is 100.00%.

❗ Current head e31ef33 differs from pull request most recent head 7f52621. Consider uploading reports for the commit 7f52621 to get more accurate results

@@            Coverage Diff             @@
##             main      #34      +/-   ##
==========================================
- Coverage   80.49%   80.41%   -0.08%     
==========================================
  Files           8        8              
  Lines         979      970       -9     
  Branches      126      125       -1     
==========================================
- Hits          788      780       -8     
+ Misses        191      190       -1     
Impacted Files Coverage Δ
src/object-hash.ts 63.30% <100.00%> (-0.59%) ⬇️

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

@pi0
Copy link
Member

pi0 commented Apr 22, 2023

Can you please resolve merge conflicts?

@H4ad H4ad force-pushed the perf/faster-circular-checking branch from e31ef33 to 7f52621 Compare April 22, 2023 11:04
@H4ad
Copy link
Contributor Author

H4ad commented Apr 22, 2023

@pi0 Fixed!

@pi0 pi0 merged commit 0128513 into unjs:main Apr 22, 2023
1 check passed
@H4ad H4ad deleted the perf/faster-circular-checking branch April 22, 2023 11:07
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.

None yet

2 participants