From dbade212890157e1b2c0dcf9ab48152e0a592cfd Mon Sep 17 00:00:00 2001 From: Kuingsmile <96409857+Kuingsmile@users.noreply.github.com> Date: Wed, 20 Aug 2025 21:04:09 +0800 Subject: [PATCH] :zap: Perf(custom): optimize random string generation algorithm --- src/renderer/manage/utils/common.ts | 33 ++++++++++++++++------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/renderer/manage/utils/common.ts b/src/renderer/manage/utils/common.ts index 07f3a892..97679b22 100644 --- a/src/renderer/manage/utils/common.ts +++ b/src/renderer/manage/utils/common.ts @@ -16,11 +16,25 @@ export const isUrlEncode = (url: string): boolean => { export const handleUrlEncode = (url: string): string => (isUrlEncode(url) ? url : encodeURI(url)) +const mask = 0b111111 +const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' export function randomStringGenerator(length: number): string { - const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' - return Array.from({ length }) - .map(() => chars.charAt(Math.floor(Math.random() * chars.length))) - .join('') + const out = new Array(length) + let i = 0 + let pool = 0 + 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 { @@ -158,17 +172,6 @@ export function isValidUrl(str: string) { } } -export const svg = ` - - ` - export function customStrMatch(str: string, pattern: string): boolean { if (!str || !pattern) return false try {