Perf(custom): optimize random string generation algorithm

This commit is contained in:
Kuingsmile
2025-08-20 21:04:09 +08:00
parent d4313f2514
commit dbade21289

View File

@@ -16,11 +16,25 @@ export const isUrlEncode = (url: string): boolean => {
export const handleUrlEncode = (url: string): string => (isUrlEncode(url) ? url : encodeURI(url)) export const handleUrlEncode = (url: string): string => (isUrlEncode(url) ? url : encodeURI(url))
const mask = 0b111111
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
export function randomStringGenerator(length: number): string { export function randomStringGenerator(length: number): string {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' const out = new Array(length)
return Array.from({ length }) let i = 0
.map(() => chars.charAt(Math.floor(Math.random() * chars.length))) let pool = 0
.join('') let bits = 0
while (i < length) {
if (bits < 6) {
pool = (pool << 30) | ((Math.random() * 0x40000000) >>> 0)
bits += 30
continue
}
const idx = pool & mask
pool >>>= 6
bits -= 6
if (idx < 62) out[i++] = chars[idx]
}
return out.join('')
} }
export function renameFileNameWithTimestamp(oldName: string): string { export function renameFileNameWithTimestamp(oldName: string): string {
@@ -158,17 +172,6 @@ export function isValidUrl(str: string) {
} }
} }
export const svg = `
<path class="path" d="
M 30 15
L 28 17
M 25.61 25.61
A 15 15, 0, 0, 1, 15 30
A 15 15, 0, 1, 1, 27.99 7.5
L 15 15
" style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"/>
`
export function customStrMatch(str: string, pattern: string): boolean { export function customStrMatch(str: string, pattern: string): boolean {
if (!str || !pattern) return false if (!str || !pattern) return false
try { try {