Skip to content

Commit 09dc39e

Browse files
committed
perf: optimize style merging performance and readability
Address code review feedback by improving the style merging logic: - Remove Set creation and array spreading for better performance - Add early exit when finding non-overlapping properties - Simplify logic flow making it more readable and maintainable - Maintain identical functionality while reducing computational overhead Performance improvements are especially beneficial for components with many style properties, reducing unnecessary object iterations.
1 parent 1a95f86 commit 09dc39e

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/native/styles/index.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,31 @@ function deepMergeConfig(
138138
// Handle style merging to support both className and inline style props
139139
let result: Record<string, any>;
140140
if (config.target) {
141-
if (Array.isArray(config.target) && config.target.length === 1 && config.target[0] === "style") {
141+
if (
142+
Array.isArray(config.target) &&
143+
config.target.length === 1 &&
144+
config.target[0] === "style"
145+
) {
142146
// Special handling for style target when we have inline styles
143147
result = { ...left, ...right };
148+
// More performant approach - check for non-overlapping properties without Sets
144149
if (left?.style && right?.style && rightIsInline) {
145-
// Only create style arrays when we have different properties that should coexist
146-
const leftKeys = new Set(Object.keys(left.style));
147-
const rightKeys = new Set(Object.keys(right.style));
148-
const hasNonOverlappingProperties = [...leftKeys].some(key => !rightKeys.has(key));
149-
150+
const leftStyle = left.style;
151+
const rightStyle = right.style;
152+
153+
// Quick check: do any left properties NOT exist in right?
154+
let hasNonOverlappingProperties = false;
155+
for (const key in leftStyle) {
156+
if (!(key in rightStyle)) {
157+
hasNonOverlappingProperties = true;
158+
break; // Early exit for performance
159+
}
160+
}
161+
150162
if (hasNonOverlappingProperties) {
151-
// Different properties exist - create array for React Native to merge both
152-
result.style = [left.style, right.style];
163+
result.style = [leftStyle, rightStyle];
153164
}
154-
// If all properties overlap, right.style will override via Object.assign above
165+
// Otherwise, Object.assign above will handle the override correctly
155166
}
156167
} else {
157168
result = Object.assign({}, left, right);

0 commit comments

Comments
 (0)