Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
Why is Math.random being outperformed by crypto.getRandomValues?

I've re-implemented Math.random() using crypto.getRandomValues with a "entropy pool" scheme to reduce the overhead of calls to crypto.getRandomValues. On my computer (x86_64 Windows Chrome) this seems to be about 260% faster than Math.random(). I find this surprising because I was under the impression that Chrome implements the latter using the blazingly fast xorshift128+ PRNG, which should be orders of magnitude faster than whatever CSPRNG is being used under the hood for the former.

class EntropyPool {
    #entropy; #index
    constructor(poolSize = 1024) {
        this.#entropy = new Uint32Array(poolSize)
        this.#index = 0
        crypto.getRandomValues(this.#entropy)
    }
    next() {
        const value = this.#entropy[this.#index++]
        if (this.#index === this.#entropy.length) {
            crypto.getRandomValues(this.#entropy)
            this.#index = 0
        }
        return value
    }
}

const pool = new EntropyPool()

function random() {
    return pool.next() / 4294967296
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda