Skip to content

Commit ff72b73

Browse files
committed
chore: use Set for checking defaultProps keys for small perf gain over 'in' operator
1 parent 6388bf9 commit ff72b73

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/components/FontAwesomeIcon.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { typedObjectKeys } from '../utils/typed-object-keys'
2222

2323
const logger = new Logger('FontAwesomeIcon')
2424

25-
const defaultProps = {
25+
const DEFAULT_PROPS = {
2626
border: false,
2727
className: '',
2828
mask: undefined,
@@ -53,6 +53,8 @@ const defaultProps = {
5353
widthAuto: false,
5454
}
5555

56+
const DEFAULT_PROP_KEYS = new Set(Object.keys(DEFAULT_PROPS))
57+
5658
export interface FontAwesomeIconProps
5759
extends Omit<SVGAttributes<SVGSVGElement>, 'children' | 'mask' | 'transform'>,
5860
RefAttributes<SVGSVGElement> {
@@ -151,7 +153,7 @@ export const FontAwesomeIcon = React.forwardRef(
151153
props: FontAwesomeIconProps,
152154
ref: React.Ref<SVGSVGElement>,
153155
): React.JSX.Element | null => {
154-
const allProps: FontAwesomeIconProps = { ...defaultProps, ...props }
156+
const allProps: FontAwesomeIconProps = { ...DEFAULT_PROPS, ...props }
155157

156158
const {
157159
icon: iconArgs,
@@ -197,12 +199,14 @@ export const FontAwesomeIcon = React.forwardRef(
197199

198200
for (const key of typedObjectKeys(allProps)) {
199201
// Skip default props
200-
if (key in defaultProps) {
202+
if (DEFAULT_PROP_KEYS.has(key)) {
201203
continue
202204
}
203205

204206
// Add all other props to the extraProps object
205-
// @ts-expect-error TypeScript doesn't know that allProps[key] is valid
207+
// @ts-expect-error since `key` can be any of the keys in FontAwesomeIconProps,
208+
// TypeScript widens the type of the `obj[key]` lookups to a union of all possible values,
209+
// which will not correctly overlap each other.
206210
extraProps[key] = allProps[key]
207211
}
208212

0 commit comments

Comments
 (0)