Skip to content

Commit 56c141b

Browse files
authored
⚡️ Faster generation of ulid (#4092)
1 parent 8f487d8 commit 56c141b

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

.yarn/versions/d7649f72.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
releases:
2+
fast-check: minor
3+
4+
declined:
5+
- "@fast-check/ava"
6+
- "@fast-check/jest"
7+
- "@fast-check/vitest"
8+
- "@fast-check/worker"

packages/fast-check/src/arbitrary/_internals/mappers/UintToBase32String.ts

+3-17
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ const decodeSymbolLookupTable: Record<string, number> = {
6060
Z: 31,
6161
};
6262

63-
/** @internal */
64-
function getBaseLog(x: number, y: number) {
65-
return Math.log(y) / Math.log(x);
66-
}
67-
6863
/** @internal */
6964
function encodeSymbol(symbol: number) {
7065
return symbol < 10 ? String(symbol) : encodeSymbolLookupTable[symbol];
@@ -81,20 +76,11 @@ function pad(value: string, paddingLength: number) {
8176

8277
/** @internal */
8378
export function uintToBase32StringMapper(num: number, paddingLength: number): string {
84-
if (num === 0) {
85-
return pad('0', paddingLength);
86-
}
87-
8879
let base32Str = '';
89-
let remaining = num;
90-
for (let symbolsLeft = Math.floor(getBaseLog(32, num)) + 1; symbolsLeft > 0; symbolsLeft--) {
91-
const val = Math.pow(32, symbolsLeft - 1);
92-
const symbol = Math.floor(remaining / val);
93-
94-
base32Str += encodeSymbol(symbol);
95-
remaining -= symbol * val;
80+
for (let remaining = num; remaining !== 0; remaining = Math.floor(remaining / 32)) {
81+
const current = remaining % 32;
82+
base32Str = encodeSymbol(current) + base32Str;
9683
}
97-
9884
return pad(base32Str, paddingLength);
9985
}
10086

packages/fast-check/test/unit/arbitrary/_internals/mappers/UintToBase32String.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {
77
describe('uintToBase32StringUnmapper', () => {
88
it('should be able to unmap any mapped value', () =>
99
fc.assert(
10-
fc.property(fc.integer({ min: 0 }), (input) => {
10+
fc.property(fc.maxSafeNat(), (input) => {
1111
// Arrange
12-
const mapped = paddedUintToBase32StringMapper(10)(input);
12+
const mapped = paddedUintToBase32StringMapper(12)(input);
1313
// Act
1414
const out = uintToBase32StringUnmapper(mapped);
1515
// Assert

0 commit comments

Comments
 (0)