perf(lib): hoist customAlphabet factory in generateId#1204
Conversation
SummaryReviewed the performance refactor in Verdict✅ Reviewed — no issues found. The change preserves the existing alphabet and |
TestingThe testing subagent classified this as an internal shared-library performance refactor with no UI, mobile, API, or integration testing required. It validated Commands run: bun --eval "/* generated 10,000 IDs and checked length, alphabet membership, and duplicates */"
bun --eval "/* benchmarked old per-call customAlphabet(alphabet)(22) pattern vs new hoisted generator over 200,000 iterations */"
bunx biome check packages/lib/generate-id.ts
bunx tsc --noEmit --skipLibCheck --moduleResolution Bundler --module ESNext --target ES2022 --strict packages/lib/generate-id.ts
bun test packages/lib/generate-id.test.ts
bunx biome check packages/lib/generate-id.ts packages/lib/generate-id.test.tsResult: No screenshots or recordings were produced because no UI, browser, or mobile testing was applicable. Verdict✅ Passed. The changed implementation preserved the expected ID shape and alphabet while showing the intended performance improvement in the testing subagent’s local benchmark. |
What & why
generateIdcallscustomAlphabet(alphabet)on every invocation, which rebuildsthe nanoid generator (re-parsing the alphabet and recomputing its internal
mask/step) on each call. The factory only needs to be created once.
This hoists the
customAlphabet(...)call to module scope so the generator isbuilt a single time and reused. Purely an optimization.
Behavior
No behavioral change. The alphabet and ID length (22) are identical, so generated
IDs keep the exact same format and distribution.
Before
ts export const generateId = () => customAlphabet("...")(22) After
ts const generate = customAlphabet("...") export const generateId = () => generate(22) Testing
bun run format-lintandbun run check-typespass forpackages/lib(pre-existing type errors in
@supermemory/toolsare unrelated to this change).Session Details
(aside)to your comment to have me ignore it.