Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
toBase62String
(version: 0)
Comparing performance of:
Array Version vs String Version
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function toBase62String1(value) { if (!value) { return value.toString(); } const base = 62n; const digits = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ]; const remainders = []; const sign = value < 0n ? "-" : ""; let quotient = sign ? 0n - value : value; do { remainders.unshift(digits[Number(quotient % base)]); quotient /= base; } while (quotient); return `${sign}${remainders.join("")}`; } function toBase62String2(value) { if (!value) { return value.toString(); } const base = 62n; const digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; const sign = value < 0n ? "-" : ""; let quotient = sign ? 0n - value : value; let result = ""; do { result = `${digits[Number(quotient % base)]}${result}`; quotient /= base; } while (quotient); return `${sign}${result}`; } function getRandomIdString1(steps = 4) { let seed = ""; for (let i = 0; i < steps; i++) { seed += Math.random().toString().substring(2); } return toBase62String1(BigInt(seed)); }; function getRandomIdString2(steps = 4) { let seed = ""; for (let i = 0; i < steps; i++) { seed += Math.random().toString().substring(2); } return toBase62String2(BigInt(seed)); };
Tests:
Array Version
getRandomIdString1()
String Version
getRandomIdString2()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array Version
String Version
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Definition** The benchmark defines two functions: `toBase62String1` and `toBase62String2`, both of which convert a BigInt value to a base-62 string. Additionally, two test cases are defined: `getRandomIdString1` and `getRandomIdString2`. These test cases generate random IDs as strings using the `toBase62String` functions. **Options Compared** The benchmark compares two implementations of the `toBase62String` function: 1. **`toBase62String1`**: This implementation uses a constant array of base-62 digits and performs division and modulo operations to convert the BigInt value to a string. 2. **`toBase62String2`**: This implementation uses a single string containing all base-62 digits, allowing for direct indexing to retrieve the correct digit. **Pros and Cons** 1. **`toBase62String1`**: * Pros: Uses a constant array, which might be more efficient in terms of memory usage. * Cons: Requires explicit division and modulo operations, which could lead to slower performance compared to using direct indexing. 2. **`toBase62String2`**: * Pros: Allows for direct indexing, potentially leading to faster performance due to reduced overhead. * Cons: Uses a larger string containing all base-62 digits, which might increase memory usage. **Library and Special JS Features** 1. **BigInt**: The `BigInt` class is used in both implementations to represent BigInt values. This feature was introduced in ECMAScript 2019 (ES2020) as part of the JavaScript standard. 2. **Math.random()**: Both test cases use `Math.random()` to generate random numbers for seed generation. **Other Considerations** The benchmark measures the performance of these two implementations, comparing their execution speed and number of executions per second. This allows users to determine which implementation is faster and more suitable for their specific use case. **Alternatives** If you're looking for alternatives or modifications to these implementations, here are a few options: 1. **Use a library**: Consider using a dedicated library like `base62` (a Node.js module) or implementing a base-62 conversion function from scratch. 2. **Optimize individual components**: Focus on optimizing specific parts of the implementation, such as reducing memory allocation for the string array in `toBase62String1`. 3. **Test with different inputs**: Test the implementations with various input sizes and types (e.g., large integers, strings) to ensure their performance characteristics hold up. Keep in mind that these alternatives might not provide significant improvements over the existing implementations but can help you explore different design choices and optimizations.
Related benchmarks:
Compare splicing methods
Slowest character conversion
Normalize digits
Phone Numbers
Comments
Confirm delete:
Do you really want to delete benchmark?