-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch from rusha to native (node:crypto) sha1 implementation (#329)
* chore: add benchmarks rusha vs native * chore: replace rusha with node:crypto * chore(node/benchmarks): reorder results * formatting * bump package.json, update changelog --------- Co-authored-by: Dylan Martin <[email protected]>
- Loading branch information
1 parent
d9c8535
commit 4402254
Showing
7 changed files
with
87 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// @ts-check | ||
|
||
import { bench, run, summary } from "mitata"; | ||
import { createHash } from "node:crypto" | ||
import pkg from 'rusha'; | ||
const { createHash: createHashRusha } = pkg; | ||
|
||
// eslint-disable-next-line | ||
const LONG_SCALE = 0xfffffffffffffff | ||
|
||
// from https://github.com/PostHog/posthog-js-lite/blob/2baa794708d78d5d10940817c3768e47abe2da99/posthog-node/src/feature-flags.ts#L460-L465 | ||
function _hashRusha(key, distinctId, salt = '') { | ||
// rusha is a fast sha1 implementation in pure javascript | ||
const sha1Hash = createHashRusha() | ||
sha1Hash.update(`${key}.${distinctId}${salt}`) | ||
return parseInt(sha1Hash.digest('hex').slice(0, 15), 16) / LONG_SCALE | ||
} | ||
|
||
function _hash(key, distinctId, salt = '') { | ||
const sha1Hash = createHash("sha1") | ||
sha1Hash.update(`${key}.${distinctId}${salt}`) | ||
return parseInt(sha1Hash.digest('hex').slice(0, 15), 16) / LONG_SCALE | ||
} | ||
|
||
summary(() => { | ||
bench("_hash with rusha", () => { | ||
_hashRusha("test", "user_id") | ||
}); | ||
|
||
bench("_hash with native", () => { | ||
_hash("test", "user_id") | ||
}); | ||
}); | ||
|
||
await run(); | ||
|
||
|
||
// !NODE | ||
// node benchmarks/rusha-vs-native.mjs | ||
// clk: ~3.99 GHz | ||
// cpu: AMD Ryzen 7 7700 8-Core Processor | ||
// runtime: node 22.11.0 (x64-linux) | ||
|
||
// benchmark avg (min … max) p75 p99 (min … top 1%) | ||
// ------------------------------------------- ------------------------------- | ||
// _hash with rusha 12.10 µs/iter 7.25 µs █ | ||
// (4.66 µs … 1.27 ms) 116.62 µs █▄▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ | ||
// _hash with native 547.04 ns/iter 435.45 ns █ | ||
// (370.08 ns … 3.61 µs) 3.58 µs █▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ | ||
|
||
// summary | ||
// _hash with native | ||
// 22.12x faster than _hash with rusha | ||
|
||
// !BUN | ||
// bun benchmarks/rusha-vs-native.mjs | ||
// clk: ~5.04 GHz | ||
// cpu: AMD Ryzen 7 7700 8-Core Processor | ||
// runtime: bun 1.1.37 (x64-linux) | ||
|
||
// benchmark avg (min … max) p75 p99 (min … top 1%) | ||
// ------------------------------------------- ------------------------------- | ||
// _hash with rusha 10.00 µs/iter 4.96 µs █ | ||
// (2.60 µs … 2.78 ms) 45.85 µs ▂█▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ | ||
// _hash with native 471.82 ns/iter 420.00 ns █ | ||
// (370.00 ns … 949.79 µs) 1.99 µs █▆▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ | ||
|
||
// summary | ||
// _hash with native | ||
// 21.19x faster than _hash with rusha |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"types": ["node", "rusha"], | ||
"typeRoots": ["./node_modules/@types", "../node_modules/@types", "./src/types"] | ||
"types": ["node"], | ||
"typeRoots": ["./node_modules/@types", "../node_modules/@types"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters