-
Notifications
You must be signed in to change notification settings - Fork 0
β‘ Bolt: [Template Filtering O(N+M) optimization] #955
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import React, { useState, useEffect } from 'react'; | ||
| import React, { useState, useEffect, useMemo } from 'react'; | ||
| import { | ||
| Box, | ||
| Card, | ||
|
|
@@ -87,9 +87,11 @@ export const TemplateSelector: React.FC<TemplateSelectorProps> = ({ | |
| ]); | ||
|
|
||
| // Filter templates on client side for excludeTemplateIds | ||
| const filteredTemplates = state.templates.filter( | ||
| (template) => !excludeTemplateIds.includes(template.id) | ||
| ); | ||
| // β‘ Bolt optimization: Use Set for O(1) lookups to convert O(N*M) array filtering to O(N+M) | ||
| const filteredTemplates = useMemo(() => { | ||
| const excludeSet = new Set(excludeTemplateIds); | ||
| return state.templates.filter((template) => !excludeSet.has(template.id)); | ||
| }, [state.templates, excludeTemplateIds]); | ||
|
Comment on lines
+91
to
+94
|
||
|
|
||
| const handleTemplateSelect = (template: BehaviorTemplate) => { | ||
| setSelectedTemplate(template); | ||
|
|
||
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.
suggestion (performance): Consider memoizing the exclusion Set separately so it only recomputes when
excludeTemplateIdschanges.Right now the
Setis rebuilt whenever eitherstate.templatesorexcludeTemplateIdschanges. Splitting this into twouseMemocalls lets you rebuild theSetonly whenexcludeTemplateIdschanges, which can help iftemplatesupdates more frequently: