Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.random vs crypto.getRandomValues (reverse order)
(version: 0)
Fast and quick test
Comparing performance of:
crypto API vs Math.random
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
crypto API
crypto.getRandomValues(new Uint8Array(1))
Math.random
Math.random()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
crypto API
Math.random
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 120 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
crypto API
110876.4 Ops/sec
Math.random
5316988.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! The provided JSON represents two individual test cases for measuring the performance of `Math.random()` and the `crypto.getRandomValues()` function. **What is being tested?** Two separate benchmarks are being compared: 1. **`Math.random()`**: This function generates a random number between 0 (inclusive) and 1 (exclusive). The benchmark measures how fast this function can generate numbers. 2. **`crypto.getRandomValues(new Uint8Array(1))`**: This function generates a cryptographically secure random byte. It's designed to provide stronger randomness than `Math.random()`. **Options compared:** The two functions are being measured in their native implementations, with no additional optimizations or tweaks applied. **Pros and Cons of the approaches:** * **`Math.random()`**: + Pros: Simple to implement, widely supported by most browsers. + Cons: Not designed for security-critical applications; can be predictable (e.g., using a fixed seed). * **`crypto.getRandomValues()`**: + Pros: Designed for security-critical applications, provides stronger randomness. + Cons: More complex to implement, may have performance overhead due to the underlying cryptographically secure algorithm. **Other considerations:** * Both functions use different algorithms to generate random numbers. `Math.random()` uses a pseudo-random number generator (PRNG), while `crypto.getRandomValues()` uses the AES-CBC-PKCS7-plain block cipher and SHA-256 hash function. * The `crypto.getRandomValues()` implementation is designed to be cryptographically secure, making it suitable for generating secrets or non-repeating numbers. **Library and purpose:** The `crypto` library is a built-in JavaScript module that provides cryptographic functions. In this benchmark, the `getRandomValues()` method is used to generate a random byte array. There are no special JavaScript features or syntax used in these benchmarks. **Other alternatives:** If you need stronger randomness than what `Math.random()` provides, you can consider using other libraries or modules, such as: * `webcrypto`: A newer API for cryptography and random number generation in browsers. * `random-bit` or `random-filler`: Third-party libraries that provide cryptographically secure random numbers. Keep in mind that these alternatives might have performance overhead or additional dependencies.
Related benchmarks:
repeated Math.random() vs crypto.getRandomValues()
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?