Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(nodejs): introduce pool into default rng #513

Merged
merged 3 commits into from
Aug 29, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/rng.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import crypto from 'crypto';

const rnds8 = new Uint8Array(16);
const poolSize = 16;
const rnds8Pool = new Uint8Array(16 * poolSize);
puzpuzpuz marked this conversation as resolved.
Show resolved Hide resolved
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));
}