Skip to content

Commit

Permalink
perf(nodejs): introduce pool into default rng (#513)
Browse files Browse the repository at this point in the history
* perf(nodejs): introduce pool into default rng

* fixup! address review comments

* fixup! address review comments
  • Loading branch information
puzpuzpuz authored Aug 29, 2020
1 parent bc9d4ed commit 7f1af04
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/rng.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import crypto from 'crypto';

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

export default function rng() {
return crypto.randomFillSync(rnds8);
if (poolPtr > rnds8Pool.length - 16) {
crypto.randomFillSync(rnds8Pool);
poolPtr = 0;
}
return rnds8Pool.slice(poolPtr, (poolPtr += 16));
}

0 comments on commit 7f1af04

Please sign in to comment.