LearnBoost/socket.io — pull request #1008 by faeldt (2012-08-28 10:26:43+00:00)
Title: Increase performance of generateId by avoiding sync calls to crypto.randomBytes

As mentioned in https://github.com/LearnBoost/socket.io/issues/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.