-
Notifications
You must be signed in to change notification settings - Fork 0
β‘ Optimize security profile lookup to O(1) #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,13 +109,17 @@ export const SECURITY_PROFILE_CATALOG: SecurityProfile[] = [ | |
|
|
||
| export const DEFAULT_SECURITY_PROFILE_ID: SecurityProfileId = 'general'; | ||
|
|
||
| const SECURITY_PROFILE_MAP = new Map<string, SecurityProfile>( | ||
| SECURITY_PROFILE_CATALOG.map((profile) => [profile.id, profile]) | ||
| ); | ||
|
Comment on lines
+112
to
+114
|
||
|
|
||
| export function getSecurityProfile(profileId?: string): SecurityProfile { | ||
| return ( | ||
| SECURITY_PROFILE_CATALOG.find((profile) => profile.id === profileId) ?? | ||
| SECURITY_PROFILE_CATALOG.find((profile) => profile.id === DEFAULT_SECURITY_PROFILE_ID)! | ||
| (profileId ? SECURITY_PROFILE_MAP.get(profileId) : undefined) ?? | ||
| SECURITY_PROFILE_MAP.get(DEFAULT_SECURITY_PROFILE_ID)! | ||
| ); | ||
|
Comment on lines
116
to
120
|
||
| } | ||
|
Comment on lines
+112
to
121
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation performs a Consider pre-resolving the default profile at the module level. This improves performance by avoiding redundant lookups and ensures that any configuration issues (like a missing default profile) are caught during module initialization rather than at runtime. const SECURITY_PROFILE_MAP = new Map<string, SecurityProfile>(
SECURITY_PROFILE_CATALOG.map((profile) => [profile.id, profile])
);
const DEFAULT_PROFILE = SECURITY_PROFILE_MAP.get(DEFAULT_SECURITY_PROFILE_ID)!;
export function getSecurityProfile(profileId?: string): SecurityProfile {
return (profileId ? SECURITY_PROFILE_MAP.get(profileId) : undefined) ?? DEFAULT_PROFILE;
} |
||
|
|
||
| export function isSecurityProfileId(profileId: string): profileId is SecurityProfileId { | ||
| return SECURITY_PROFILE_CATALOG.some((profile) => profile.id === profileId); | ||
| return SECURITY_PROFILE_MAP.has(profileId); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SECURITY_PROFILE_MAPis built once fromSECURITY_PROFILE_CATALOG, butSECURITY_PROFILE_CATALOGis exported as a mutableSecurityProfile[]. If any consumer mutates the exported catalog at runtime, lookups viagetSecurityProfile/isSecurityProfileIdwill silently diverge from the catalog. Consider making the catalog immutable (e.g.,ReadonlyArrayand/or freezing) or otherwise ensuring the exported list cannot be mutated after module init.