Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Generate random string
(version: 0)
Comparing performance of:
Test gen1 vs Test gen2
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function gen1() { return Math.random().toString(36).slice(2) } 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() / 4294967295 } const lowercase = 'abcdefghijklmnopqrstuvwxyz'; const uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; const digits = '0123456789'; const charset = `${lowercase}${uppercase}${digits}`; function gen2() { let result = ''; for (let i = 0; i < length; i++) { const id = randomInt([0, charset.length - 1]); result += charset[id]; } return result; }
Tests:
Test gen1
gen1()
Test gen2
gen2()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Test gen1
Test gen2
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/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Test gen1
2377419.0 Ops/sec
Test gen2
3494086.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the provided benchmark and its components. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that measures the performance of two different functions: `gen1()` and `gen2()`. The benchmark definition is as follows: * **Script Preparation Code**: This section contains the code that sets up the environment for the benchmark. In this case, it defines a class `EntropyPool` that generates random numbers using the `crypto.getRandomValues()` function. * **Html Preparation Code**: This section is empty, indicating that no HTML-specific setup is required. **Test Cases** The benchmark consists of two test cases: 1. **Test gen1**: Measures the performance of the `gen1()` function, which generates a random string by converting a random number to a base-36 string using `Math.random().toString(36).slice(2)`. 2. **Test gen2**: Measures the performance of the `gen2()` function, which generates a random string by iterating over a charset and selecting a random character from it. **Options Compared** In this benchmark, two different approaches are compared: 1. **Approach 1: Using EntropyPool (gen1())**: This approach uses an `EntropyPool` class to generate random numbers. The pool is initialized with a fixed number of entropy values, and the `next()` function returns the next available value. 2. **Approach 2: Manual Character Selection (gen2())**: This approach manually selects characters from a charset using the `randomInt()` function, which generates a random index into the charset. **Pros and Cons** Here are some pros and cons of each approach: 1. **Using EntropyPool (gen1())**: * Pros: Efficient use of entropy values, reduces overhead of generating random numbers. * Cons: Limited control over character selection, may not be suitable for all use cases. 2. **Manual Character Selection (gen2())**: * Pros: Provides more control over character selection, can be tailored to specific requirements. * Cons: May lead to slower performance due to the overhead of generating random indices. **Other Considerations** When choosing between these approaches, consider the following factors: * Performance requirements: If speed is crucial, using an EntropyPool class may provide better performance. * Character distribution: If a specific character distribution is required, manual character selection may be more suitable. * Code readability and maintainability: Manual character selection can lead to more complex code, while using an EntropyPool class may result in simpler code. **Library Used** The `EntropyPool` class uses the `crypto.getRandomValues()` function from Node.js's built-in crypto module. This library is used to generate cryptographically secure random numbers. **Special JS Feature or Syntax** None of the benchmark code uses any special JavaScript features or syntax, such as ES6 modules, async/await, or WebAssembly. I hope this explanation helps software engineers understand the provided benchmark and its components!
Related benchmarks:
LIS-test2
Object, Map: string vs numeric smi keys access performance
Fisher-Yates Shuffle
shuffle array [dsng-manscaped]
orderBy vs array.prototype.sort vs vanila orderBy vs QuickSort
Comments
Confirm delete:
Do you really want to delete benchmark?