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));
};
getRandomIdString1()
getRandomIdString2()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array Version | |
String Version |
Test name | Executions per second |
---|---|
Array Version | 38835.8 Ops/sec |
String Version | 48588.9 Ops/sec |
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:
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.toBase62String2
: This implementation uses a single string containing all base-62 digits, allowing for direct indexing to retrieve the correct digit.Pros and Cons
toBase62String1
:toBase62String2
:Library and Special JS Features
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.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:
base62
(a Node.js module) or implementing a base-62 conversion function from scratch.toBase62String1
.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.