-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.ts
64 lines (60 loc) · 1.92 KB
/
bench.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { hash as rustWasmHash } from "./blake3-wasm/pkg/blake3_wasm.js";
import { sha256 } from "https://denopkg.com/chiefbiiko/[email protected]/mod.ts";
import { hash as jsHashV0 } from "./js/v0.ts";
import { hash as jsHashV1 } from "./js/v1.ts";
import { hash as jsHashV2 } from "./js/v2.ts";
import { hash as jsHashV3 } from "./js/v3.ts";
import { hash as jsHashV4 } from "./js/v4.ts";
import { hash as jsHashV5 } from "./js/v5.ts";
import { hash as jsHashV6 } from "./js/v6.ts";
import { hash as jsHashV7 } from "./js/v7.ts";
import { hash as jsHashV8 } from "./js/v8.ts";
import { hash as jsHashV9 } from "./js/v9.ts";
import { hash as latestHash } from "./js/latest.ts";
// Share the same input buffer across benchmars.
const INPUT_BUFFER = new Uint8Array(1024 * 1024);
const BENCH_CASES = (<[string, number][]>[
["96B", 96],
["512B", 512],
["1Kib", 1 * 1024],
["32Kib", 32 * 1024],
["64Kib", 64 * 1024],
["256Kib", 256 * 1024],
["1MB", 1024 * 1024],
]).map(([humanSize, length]) => {
// Slice up to `length` many bytes from the shared array.
const array = new Uint8Array(INPUT_BUFFER.buffer, 0, length);
return <[string, Uint8Array]>[humanSize, array];
});
// Randomize the input.
for (let i = 0; i < INPUT_BUFFER.length; ) {
let rng = Math.random() * Number.MAX_SAFE_INTEGER;
for (let j = 0; j < 4; ++j) {
INPUT_BUFFER[i++] = rng & 0xff;
rng >>= 8;
}
}
function bench(name: string, fn: (array: Uint8Array) => any) {
for (const [group, input] of BENCH_CASES) {
Deno.bench({
name: `${name} ${group}`,
group,
fn() {
fn(input);
},
});
}
}
bench("Sha256", sha256);
bench("Rust (wasm)", rustWasmHash);
bench("Js#01", jsHashV0);
bench("Js#02", jsHashV1);
bench("Js#03", jsHashV2);
bench("Js#04", jsHashV3);
bench("Js#05", jsHashV4);
bench("Js#06", jsHashV5);
bench("Js#07", jsHashV6);
bench("Js#08", jsHashV7);
bench("Js#09", jsHashV8);
bench("JS#10", jsHashV9);
bench("Js#11", latestHash);