Skip to content

Commit 04a1a07

Browse files
committed
perf: reduce memo and lazy mount
1 parent caa8ec7 commit 04a1a07

7 files changed

Lines changed: 32 additions & 16 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@chakra-ui/react": patch
3+
---
4+
5+
- **Styled System:** Boost performance of style resolution by removing
6+
`JSON.stringify` in `memo` function and avoid memoizing non-primitive
7+
arguments.
8+
9+
- **Menu, Tooltip:** Set `lazyMount` and `unmountOnExit` to `true` in the `Root`
10+
component to improve initial rendering performance.

packages/react/__stories__/sandbox.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const ColorPalette = () => {
6969
export const SelfClosing = () => {
7070
return (
7171
<Box rounded="full" overflow="hidden" asChild>
72-
<img src="https://via.placeholder.com/150" alt="placeholder" />
72+
<img src="https://i.pravatar.cc/150?u=1" alt="placeholder" />
7373
</Box>
7474
)
7575
}

packages/react/src/components/menu/menu.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export interface MenuRootProps extends MenuRootBaseProps {
4444
children: React.ReactNode
4545
}
4646

47-
export const MenuRoot = withRootProvider<MenuRootProps>(ArkMenu.Root)
47+
export const MenuRoot = withRootProvider<MenuRootProps>(ArkMenu.Root, {
48+
defaultProps: { lazyMount: true, unmountOnExit: true },
49+
})
4850

4951
////////////////////////////////////////////////////////////////////////////////////
5052

packages/react/src/components/tooltip/tooltip.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export interface TooltipRootProps extends TooltipRootBaseProps {
4444
children?: React.ReactNode
4545
}
4646

47-
export const TooltipRoot = withRootProvider<TooltipRootProps>(ArkTooltip.Root)
47+
export const TooltipRoot = withRootProvider<TooltipRootProps>(ArkTooltip.Root, {
48+
defaultProps: { lazyMount: true, unmountOnExit: true },
49+
})
4850

4951
////////////////////////////////////////////////////////////////////////////////////
5052

packages/react/src/styled-system/css.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
compact,
44
isObject,
55
isString,
6-
memo,
76
mergeWith,
87
walkObject,
98
} from "../utils"
@@ -28,7 +27,7 @@ export function createCssFn(context: CssFnOptions) {
2827
const { transform, conditions, normalize } = context
2928
const mergeFn = mergeCss(context)
3029

31-
return memo((...styleArgs: SystemStyleObject[]) => {
30+
return function cssFn(...styleArgs: SystemStyleObject[]) {
3231
const styles = mergeFn(...styleArgs)
3332

3433
const normalized = normalize(styles)
@@ -58,7 +57,7 @@ export function createCssFn(context: CssFnOptions) {
5857
})
5958

6059
return sortAtRules(result)
61-
})
60+
}
6261
}
6362

6463
function mergeByPath(target: Dict, paths: string[], value: Dict) {
@@ -83,7 +82,7 @@ function mergeCss(ctx: CssFnOptions) {
8382
if (comp.length === 1) return comp
8483
return comp.map((style) => ctx.normalize(style))
8584
}
86-
return memo((...styles) => {
85+
return function mergeFn(...styles: Dict[]) {
8786
return mergeWith({}, ...resolve(styles))
88-
})
87+
}
8988
}

packages/react/src/styled-system/utility.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,17 @@ export function createUtility(options: Options) {
159159
return values
160160
}
161161

162-
const defaultTransform = memo((prop: string, value: string) => {
162+
const defaultTransform = (prop: string, value: string) => {
163163
return {
164164
[prop]: prop.startsWith("--") ? tokens.getVar(value, value) : value,
165165
}
166-
})
166+
}
167167

168168
const tokenFn = Object.assign(tokens.getVar, {
169169
raw: (path: string) => tokens.getByName(path),
170170
})
171171

172-
const transform = memo((prop: string, raw: any) => {
172+
const transform = (prop: string, raw: any) => {
173173
const key = resolveShorthand(prop)
174174

175175
// skip emotion generated keyframes
@@ -194,7 +194,7 @@ export function createUtility(options: Options) {
194194
token: tokenFn,
195195
utils: { colorMix: _colorMix },
196196
})
197-
})
197+
}
198198

199199
function build() {
200200
assignShorthands()
@@ -207,9 +207,9 @@ export function createUtility(options: Options) {
207207

208208
const hasShorthand = shorthands.size > 0
209209

210-
const resolveShorthand = memo((prop: string) => {
210+
const resolveShorthand = (prop: string) => {
211211
return shorthands.get(prop) ?? prop
212-
})
212+
}
213213

214214
const keys = () => {
215215
return [...Array.from(shorthands.keys()), ...Object.keys(configs)]

packages/react/src/utils/memo.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
export const memo = <T extends (...args: any[]) => any>(fn: T): T => {
1+
export const memo = <T extends (...args: any[]) => any>(
2+
fn: T,
3+
getKey?: (...args: any[]) => string,
4+
): T => {
25
const cache: Record<string, any> = Object.create(null)
36
function get(...args: any[]) {
4-
const key = JSON.stringify(args)
7+
const key = getKey?.(...args) ?? args.join("|")
58
if (cache[key] === undefined) cache[key] = fn(...args)
69
return cache[key]
710
}

0 commit comments

Comments
 (0)