-
Notifications
You must be signed in to change notification settings - Fork 23
Perf: Cache styles to avoid creating new object style every execution #250
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
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -173,16 +173,36 @@ export class StyleBuilder< | |||||||
| definitions: K[], | ||||||||
| conditionalDefinitions: (ConditionalK | null | undefined | boolean)[] = [] | ||||||||
| ): UnionToIntersection<D[K]> & Partial<UnionToIntersection<D[ConditionalK]>> { | ||||||||
| const styles: any[] = []; | ||||||||
| const allDefinitions = [ | ||||||||
| ...(definitions || []), | ||||||||
| ...(conditionalDefinitions || []), | ||||||||
| ].filter((definition) => typeof definition === "string"); | ||||||||
| const sortedDefinitions = allDefinitions.sort(); | ||||||||
| // Since definitions are sorted, the order of input doesn't matter now -> same styles will have the same keys | ||||||||
| const keyStyle = sortedDefinitions.reduce( | ||||||||
| (prevStyle, currStyle) => `${prevStyle}${currStyle}`, | ||||||||
| "" | ||||||||
| ); | ||||||||
| let styles = this.cached.get(keyStyle); | ||||||||
| if (styles) return styles; | ||||||||
|
|
||||||||
|
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. Ensure styles initialization. Before merging styles, ensure that the - let styles = this.cached.get(keyStyle);
- if (styles) return styles;
+ let styles = this.cached.get(keyStyle) || {};
+ if (Object.keys(styles).length > 0) return styles;Committable suggestion
Suggested change
|
||||||||
| for (const definition of definitions) { | ||||||||
| styles.push(this.get<D, K>(definition)); | ||||||||
| styles = { | ||||||||
| ...(styles || {}), | ||||||||
| ...(this.get<D, K>(definition) || {}), | ||||||||
| }; | ||||||||
| } | ||||||||
| for (const definition of conditionalDefinitions) { | ||||||||
| if (definition && definition !== true) { | ||||||||
| styles.push(this.get<D, K>(definition as unknown as K)); | ||||||||
| styles = { | ||||||||
| ...(styles || {}), | ||||||||
| ...(this.get<D, K>(definition as unknown as K) || {}), | ||||||||
| }; | ||||||||
| } | ||||||||
| } | ||||||||
| return StyleSheet.flatten(styles); | ||||||||
|
|
||||||||
| this.cached.set(keyStyle, styles); | ||||||||
| return styles; | ||||||||
| } | ||||||||
|
|
||||||||
| get< | ||||||||
|
|
||||||||
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.
Ensure definitions are not null or undefined.
The code concatenates
definitionsandconditionalDefinitionsand filters out non-string values. Ensure thatdefinitionsandconditionalDefinitionsare not null or undefined before concatenation.Committable suggestion