Skip to content

Commit 7f1af04

Browse files
authored
perf(nodejs): introduce pool into default rng (#513)
* perf(nodejs): introduce pool into default rng * fixup! address review comments * fixup! address review comments
1 parent bc9d4ed commit 7f1af04

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/rng.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import crypto from 'crypto';
22

3-
const rnds8 = new Uint8Array(16);
3+
const rnds8Pool = new Uint8Array(256); // # of random values to pre-allocate
4+
let poolPtr = rnds8Pool.length;
45

56
export default function rng() {
6-
return crypto.randomFillSync(rnds8);
7+
if (poolPtr > rnds8Pool.length - 16) {
8+
crypto.randomFillSync(rnds8Pool);
9+
poolPtr = 0;
10+
}
11+
return rnds8Pool.slice(poolPtr, (poolPtr += 16));
712
}

0 commit comments

Comments
 (0)