Skip to content

Commit

Permalink
chore: to thi.ng
Browse files Browse the repository at this point in the history
  • Loading branch information
maraisr committed Apr 27, 2023
1 parent f7dd2e3 commit 97d8c63
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 93 deletions.
93 changes: 93 additions & 0 deletions bench/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { createHash } from 'crypto';
import objectHash from 'object-hash';
import { identify } from 'object-identity';

import { EMPTY, suite } from '@thi.ng/bench';

const getObject = () => {
const c = [1];
// @ts-ignore
c.push(c);
return { a: { b: ['c', new Set(['d', new Map([['e', 'f']]), c, 'g'])] } };
};

const contenders = {
['object-identity']() {
return (o) => identify(o);
},
['object-hash']() {
const options = { algorithm: 'passthrough', unorderedSets: false };

return (o) => objectHash(o, options);
},

['object-identity :: hashed']() {
const hasher = (val) => createHash('sha1').update(val).digest('hex');

return (o) => identify(o, hasher);
},
['object-hash :: hashed']() {
const options = {
algorithm: 'sha1',
encoding: 'hex',
unorderedSets: false,
};

return (o) => objectHash(o, options);
},
};

runner(contenders);

async function runner(contenders) {
const cases = [];
for (const [name, contender] of Object.entries(contenders)) {
const run = contender();
const o = getObject();
const fn = () => run(o);
cases.push({ fn, title: name });
}

suite(cases, {
warmup: 300,
size: 50,
iter: 10_000,
format: FORMAT(),
});
}

function FORMAT() {
const formatter = new Intl.NumberFormat('en-US');
return {
prefix: EMPTY,
suffix: EMPTY,
start: EMPTY,
warmup: EMPTY,
result: EMPTY,
total(a) {
const winner = a.slice().sort((a, b) => a.mean - b.mean)[0];
const compute = a.map((x) => {
return {
title: x.title,
won: x === winner,
ops: formatter.format(
Math.floor((x.iter * x.size) / (x.total / 1000)),
),
sd: (x.sd / 1000).toFixed(2),
};
});
const max_name = Math.max(...a.map((x) => x.title.length));
const max_ops = Math.max(...compute.map((x) => x.ops.length));
const lines = [];
for (const x of compute) {
const won = x.won ? '★ ' : ' ';
lines.push(
`${won}${x.title.padEnd(max_name)} ~ ${x.ops.padStart(
max_ops,
)} ops/sec ± ${x.sd}%`,
);
}
return lines.join('\n');
},
};
}
71 changes: 0 additions & 71 deletions bench/index.ts

This file was deleted.

8 changes: 4 additions & 4 deletions bench/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"private": true,
"type": "module",
"dependencies": {
"@types/benchmark": "2.1.2",
"@types/node": "^18.16.1",
"@thi.ng/bench": "^3.2.10",
"benchmark": "2.1.4",
"object-hash": "^3.0.0"
"object-hash": "^3.0.0",
"object-identity": "file:.."
}
}
30 changes: 20 additions & 10 deletions bench/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"index.d.ts"
],
"scripts": {
"bench": "node -r tsm bench/index.ts",
"bench": "node bench/index.js",
"build": "bundt --minify",
"test": "uvu test -r tsm \".spec.ts$\"",
"typecheck": "tsc --noEmit"
Expand Down
11 changes: 4 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,10 @@ assert.toEqual(hashA, hashB);
> via the [`/bench`](/bench) directory with Node v16.20.0 (Apple M1 Pro)
```
benchmark :: hashed
object-hash x 58,395 ops/sec ±0.23% (96 runs sampled)
object-identity x 1,180,665 ops/sec ±0.44% (96 runs sampled)
benchmark :: passed through
object-hash x 123,699 ops/sec ±0.22% (99 runs sampled)
object-identity x 1,188,861 ops/sec ±0.21% (99 runs sampled)
object-identity ~ 1,759,250 ops/sec ± 0.03%
object-hash ~ 137,578 ops/sec ± 0.01%
★ object-identity :: hashed ~ 1,842,540 ops/sec ± 0.02%
object-hash :: hashed ~ 58,219 ops/sec ± 0.01%
```

> ^ `object-identity` is not as feature-full it's alternatives, specifically around `function` values and other node
Expand Down

0 comments on commit 97d8c63

Please sign in to comment.