Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Increase performance of generateId by avoiding sync calls to crypto.randomBytes #1008
As mentioned in LearnBoost#934, generateId contains a sync call to crypto.randomBytes. To try to minimize the impact of this, I've added a new function called getRandomBytes that gets bytes in chunks using async calls and keeps them in a buffer to deliver when needed, graciously falling back to the sync call only when the buffer is temporarily empty. This result in a significant decrease in blocking calls compared to the current implementation.
I tried making 100k calls to this.getRandomBytes(16) (one per process.nextTick()), and out of these only about 3k fell through to the sync crypto.randomBytes(), while the rest were delivered from the buffer.
The average return time of this.getRandomBytes(16) is roughly 0.00100, compared to a crypto.randomBytes(16) call which clocks in at an average of about 0.00600.