Crypto.getRandomValues vs Math.random speed (v2)

Revision 2 of this benchmark created on


Description

They are not the same. Crypto.getRandomValues generates crypto-safe random numbers. While Math.random is not crypto-safe AT ALL. But how do they differ in speed?

Setup

function crypto_rand(num) {
  const arr = new Uint32Array(num * 2)
  self.crypto.getRandomValues(arr)
  return arr
}

function math_rand(num) {
  const arr = new ArrayBuffer(8 * num)
  const view = new DataView(arr)
  for (let i = 0; i < num; i++) {
    const val = Math.random()
    view.setFloat64(8 * i, val)
  }
  return new Uint32Array(arr);
}

Test runner

Ready to run.

Testing in
TestOps/sec
Uint32*2 with Crypto
crypto_rand(50)
ready
Uint32*2 with Math
math_rand(50)
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.