Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
repeated Math.random() vs crypto.getRandomValues()
(version: 0)
Comparing performance of:
Math.random() vs crypto.getRandomValues()
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var v = new Uint32Array(16384);
Tests:
Math.random()
for(let i = 0; i < 16384; ++i) v[i] = 4294967296 * Math.random();
crypto.getRandomValues()
crypto.getRandomValues(v);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Math.random()
crypto.getRandomValues()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.random()
172.2 Ops/sec
crypto.getRandomValues()
26528.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what is being tested in the provided JSON benchmark. **What is being tested:** The benchmark measures the performance difference between two approaches to generate random numbers: 1. **Math.random()**: This is a built-in JavaScript function that generates a random number between 0 (inclusive) and 1 (exclusive). It's used extensively in web development for generating pseudo-random numbers. 2. **crypto.getRandomValues()**: This is a Web Cryptography API function that generates cryptographically secure pseudo-random numbers. **Options compared:** The benchmark compares the performance of these two approaches using the same input data: * For each approach, 16,384 (16384) random values are generated and stored in a `Uint32Array` called `v`. * The number of executions per second is measured for each approach over multiple runs. **Pros and cons:** 1. **Math.random()**: This approach has the advantage of being extremely simple to implement and understand, as it's a built-in JavaScript function. However, its performance can be poor when generating large numbers of random values in sequence, as it relies on an algorithm that is not designed for high-performance use. 2. **crypto.getRandomValues()**: This approach provides cryptographically secure pseudo-random numbers, which are suitable for applications requiring high security, such as encryption and random number generation. However, its implementation can be more complex than `Math.random()`, especially when dealing with different platforms. **Other considerations:** * The benchmark assumes that the input data (16,384 values) is large enough to demonstrate a significant performance difference between the two approaches. * The use of a `Uint32Array` as the output buffer ensures that the generated random numbers are uniformly distributed and have the same range. * The benchmark doesn't account for potential differences in performance due to platform-specific optimizations or caching. **Library/libraries:** None explicitly mentioned, but the Web Cryptography API (`crypto`) is used to generate cryptographically secure pseudo-random numbers. **Special JS feature/syntax:** Not applicable in this simple benchmark. Now that we've broken down what's being tested, let's talk about alternative approaches: 1. **other algorithms**: Other random number generation algorithms, such as the Mersenne Twister or the Linear Congruential Generator, could be used instead of `Math.random()` and `crypto.getRandomValues()`. 2. **Non-Web Cryptography API methods**: For generating cryptographically secure pseudo-random numbers, other APIs like the Windows API (in .NET) or the OpenSSL library (in C++) could be used. 3. **Library alternatives**: Other libraries, such as the `random-js` library, could be used to generate random numbers instead of relying on built-in JavaScript functions. Keep in mind that these alternative approaches would likely have different pros and cons compared to the original benchmark.
Related benchmarks:
Math.random vs crypto.getRandom
Math.random vs crypto.getRandom Alberto
crypto.getRandomValues vs Math.random()
Math.floor(Math.random()) vs crypto.getRandom
Comments
Confirm delete:
Do you really want to delete benchmark?