-
Notifications
You must be signed in to change notification settings - Fork 578
/
bench.ts
78 lines (69 loc) · 2.33 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env -S deno run
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { bench, runBenchmarks } from "../../testing/bench.ts";
import { createHash } from "../../hash/mod.ts";
import { assert, assertEquals } from "../../testing/asserts.ts";
import { crypto as stdCrypto } from "../mod.ts";
const webCrypto = globalThis.crypto;
// WASM is limited to 32-bit operations, which SHA-256 is optimized for, while
// SHA-512 is optimized for 64-bit operations and may be slower.
for (const algorithm of ["SHA-256", "SHA-512"] as const) {
for (
const length of [
64,
262_144,
4_194_304,
67_108_864,
524_291_328,
] as const
) {
// Create a test input buffer and do some operations to hopefully ensure
// it's fully initialized and not optimized away.
const buffer = new Uint8Array(length);
for (let i = 0; i < length; i++) {
buffer[i] = ((i + i % 13) + i % 31) % 255;
}
let sum = 0;
for (const byte of buffer) {
sum += byte;
}
assert(sum > 0);
for (
const implementation of [
"runtime WebCrypto (target)",
"std@0.102.0/hash WASM (baseline)",
"std/crypto WASM (you are here)",
] as const
) {
let lastDigest: ArrayBuffer | undefined;
bench({
name: `${algorithm.padEnd(12)} ${
length.toString().padStart(12)
}B ${implementation}`,
async func(timer) {
timer.start();
let digest;
if (implementation === "std/crypto WASM (you are here)") {
digest = stdCrypto.subtle.digestSync(algorithm, buffer);
} else if (implementation === "runtime WebCrypto (target)") {
digest = await webCrypto.subtle.digest(algorithm, buffer);
} else if (implementation === "std@0.102.0/hash WASM (baseline)") {
digest = createHash(
algorithm.toLowerCase().replace("-", "") as "sha256" | "sha512",
).update(buffer).digest();
} else {
throw new Error(`Unknown implementation ${implementation}`);
}
timer.stop();
assert(digest.byteLength > 0);
if (lastDigest) {
assertEquals(lastDigest, digest);
}
lastDigest = digest;
},
runs: 10,
});
}
}
}
await runBenchmarks();