Skip to content
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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/mui-utils/src/composeClasses/composeClasses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('composeClasses', () => {
undefined,
),
).to.deep.equal({
root: 'MuiTest-root MuiTest-standard',
slot: 'MuiTest-slot',
root: 'MuiTest-root MuiTest-standard ',
slot: 'MuiTest-slot ',
});
});

Expand All @@ -32,8 +32,8 @@ describe('composeClasses', () => {
},
),
).to.deep.equal({
root: 'MuiTest-root MuiTest-standard standardOverride',
slot: 'MuiTest-slot slotOverride',
root: 'MuiTest-root MuiTest-standard standardOverride ',
slot: 'MuiTest-slot slotOverride ',
});
});

Expand All @@ -51,8 +51,8 @@ describe('composeClasses', () => {
},
),
).to.deep.equal({
root: 'MuiTest-root MuiTest-standard standardOverride',
slot: 'MuiTest-slot slotOverride',
root: 'MuiTest-root MuiTest-standard standardOverride ',
slot: 'MuiTest-slot slotOverride ',
});
});
});
75 changes: 57 additions & 18 deletions packages/mui-utils/src/composeClasses/composeClasses.ts
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;
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough

}
}
}

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;
}
Loading