-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[utils] Add small optimization: composeClasses #41488
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,69 @@ | ||
/* eslint no-restricted-syntax: 0, prefer-template: 0, no-unreachable-loop: 0 | ||
--- | ||
These rules are preventing the performance optimizations below. | ||
*/ | ||
|
||
export default function composeClasses<ClassKey extends string>( | ||
slots: Record<ClassKey, ReadonlyArray<string | false | undefined | null>>, | ||
getUtilityClass: (slot: string) => string, | ||
classes: Record<string, string> | undefined = undefined, | ||
): Record<ClassKey, string> { | ||
const output: Record<ClassKey, string> = {} as any; | ||
const output = {} as Record<ClassKey, string>; | ||
|
||
/* This is split in 2 loops for performance reasons, as it allows to avoid the `if (classes)` | ||
* check in both of those cases. If `classes` is either `{}` or `undefined`, we consider it empty. */ | ||
if (isEmpty(classes)) { | ||
for (const slotName in slots) { | ||
if (Object.prototype.hasOwnProperty.call(slots, slotName)) { | ||
const slotNames = slots[slotName]; | ||
|
||
Object.keys(slots).forEach( | ||
// `Object.keys(slots)` can't be wider than `T` because we infer `T` from `slots`. | ||
// @ts-expect-error https://github.com/microsoft/TypeScript/pull/12253#issuecomment-263132208 | ||
(slot: ClassKey) => { | ||
output[slot] = slots[slot] | ||
.reduce((acc, key) => { | ||
if (key) { | ||
const utilityClass = getUtilityClass(key); | ||
if (utilityClass !== '') { | ||
acc.push(utilityClass); | ||
let result = ''; | ||
for (let i = 0; i < slotNames.length; i += 1) { | ||
const slotPart = slotNames[i]; | ||
if (slotPart) { | ||
const utilityClass = getUtilityClass(slotPart); | ||
if (utilityClass) { | ||
result += utilityClass + ' '; | ||
} | ||
if (classes && classes[key]) { | ||
acc.push(classes[key]); | ||
} | ||
} | ||
output[slotName] = result; | ||
} | ||
} | ||
} else { | ||
for (const slotName in slots) { | ||
if (Object.prototype.hasOwnProperty.call(slots, slotName)) { | ||
const slotNames = slots[slotName]; | ||
|
||
let result = ''; | ||
for (let i = 0; i < slotNames.length; i += 1) { | ||
const slotPart = slotNames[i]; | ||
if (slotPart) { | ||
const utilityClass = getUtilityClass(slotPart); | ||
if (utilityClass) { | ||
result += utilityClass + ' '; | ||
} | ||
if (classes[slotPart]) { | ||
result += classes[slotPart] + ' '; | ||
} | ||
} | ||
return acc; | ||
}, [] as string[]) | ||
.join(' '); | ||
}, | ||
); | ||
} | ||
output[slotName] = result; | ||
} | ||
} | ||
} | ||
|
||
return output; | ||
} | ||
|
||
function isEmpty(value: object | undefined): value is undefined { | ||
if (!value) { | ||
return true; | ||
} | ||
for (const key in value) { | ||
if (Object.prototype.hasOwnProperty.call(value, key)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Would
output[slotName] = result.trim()
(to remove the trailing space) defeat the optimization's purpose?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.
It would, the V8
ConsString
I linked above is efficient because it doesn't need to copy+write the string to perform concatenation, it can simply use linked-list (or linked-tree) operations. Trimming it however requires to 1. turn the string into an actual array of bytes, and 2. create a trimmed copy of those bytes. Considering that this is for classnames, it feels very acceptable to leave additional whitespaces in.You can see the performance cost here.
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.
Fair enough