Skip to content

perf(lib): hoist customAlphabet factory in generateId#1204

Open
kbpr21 wants to merge 1 commit into
supermemoryai:mainfrom
kbpr21:fix/generate-id-hoist-factory
Open

perf(lib): hoist customAlphabet factory in generateId#1204
kbpr21 wants to merge 1 commit into
supermemoryai:mainfrom
kbpr21:fix/generate-id-hoist-factory

Conversation

@kbpr21

@kbpr21 kbpr21 commented Jul 5, 2026

Copy link
Copy Markdown

What & why

generateId calls customAlphabet(alphabet) on every invocation, which rebuilds
the 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 is
built 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-lint and bun run check-types pass for packages/lib
(pre-existing type errors in @supermemory/tools are unrelated to this change).


Session Details

  • Session: View Session
  • Requested by: Unknown
  • Address comments on this PR. Add (aside) to your comment to have me ignore it.

@vorflux

vorflux Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Reviewed the performance refactor in packages/lib/generate-id.ts, which hoists the customAlphabet generator used by generateId. The review subagent checked the diff against main for correctness, security, breaking changes, race conditions, and behavioral regressions.

Verdict

Reviewed — no issues found. The change preserves the existing alphabet and 22 character length while avoiding repeated generator construction.

@vorflux

vorflux Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Testing

The testing subagent classified this as an internal shared-library performance refactor with no UI, mobile, API, or integration testing required. It validated generateId runtime invariants, compared the old and new generator patterns, checked formatting/linting for the changed file, and ran a focused TypeScript check.

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.ts

Result:

Runtime invariant check: PASS
- 10,000 generated IDs
- every ID length: 22
- alphabet: unchanged 58-character alphabet only
- duplicateCount: 0

Performance comparison: PASS
- old per-call pattern: 105.61ms
- new hoisted generator: 90.51ms
- observed speedup: ~1.17x

Changed-file Biome: PASS
- Checked 1 file in 126ms. No fixes applied.

Product-file TypeScript check: PASS

Focused local unit test validation: PASS
- 1 pass
- 0 fail
- 3 expect() calls

Biome on changed file plus local test file: PASS
- Checked 2 files in 10ms. No fixes applied.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant