-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathnonce.js
More file actions
45 lines (40 loc) · 1.12 KB
/
nonce.js
File metadata and controls
45 lines (40 loc) · 1.12 KB
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
/* eslint-disable */
import benchmark from 'benchmark'
import { Nonce } from '../dist/src/@types/nonce.js'
/**
* Using Nonce class is 150x faster than nonceToBytes
* nonceToBytes x 2.25 ops/sec ±1.41% (10 runs sampled)
* Nonce class x 341 ops/sec ±0.71% (87 runs sampled)
*/
function nonceToBytes (n) {
// Even though we're treating the nonce as 8 bytes, RFC7539 specifies 12 bytes for a nonce.
const nonce = new Uint8Array(12)
new DataView(nonce.buffer, nonce.byteOffset, nonce.byteLength).setUint32(4, n, true)
return nonce
}
const main = function () {
const bench1 = new benchmark('nonceToBytes', {
fn: function () {
for (let i = 1e6; i < 2 * 1e6; i++) {
nonceToBytes(i)
}
}
})
.on('complete', function (stats) {
console.log(String(stats.currentTarget))
})
bench1.run()
const bench2 = new benchmark('Nonce class', {
fn: function () {
const nonce = new Nonce(1e6)
for (let i = 1e6; i < 2 * 1e6; i++) {
nonce.increase()
}
}
})
.on('complete', function (stats) {
console.log(String(stats.currentTarget))
})
bench2.run()
}
main()