Skip to content

Commit c155796

Browse files
Put common aliases in Map/Set instead of switch over strings (#26551)
This is a follow up to #26546 This is strictly a perf optimization since we know that switches over strings aren't optimally implemented in current engines. Basically they're a sequence of ifs. As a result, we're better off putting the unusual cases in a Map and the very common cases in the beginning of the switch. We might be better off putting very common cases in explicit ifs - just in case the engine does optimize switches to a hash table which is potentially worse. --------- Co-authored-by: Sophie Alpert <git@sophiebits.com>
1 parent d5fd60f commit c155796

File tree

5 files changed

+492
-1447
lines changed

5 files changed

+492
-1447
lines changed

packages/react-dom-bindings/src/client/DOMPropertyOperations.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,33 @@ export function setValueForAttribute(
140140
}
141141
}
142142

143+
export function setValueForKnownAttribute(
144+
node: Element,
145+
name: string,
146+
value: mixed,
147+
) {
148+
if (value === null) {
149+
node.removeAttribute(name);
150+
return;
151+
}
152+
switch (typeof value) {
153+
case 'undefined':
154+
case 'function':
155+
case 'symbol':
156+
case 'boolean': {
157+
node.removeAttribute(name);
158+
return;
159+
}
160+
}
161+
if (__DEV__) {
162+
checkAttributeStringCoercion(value, name);
163+
}
164+
node.setAttribute(
165+
name,
166+
enableTrustedTypesIntegration ? (value: any) : '' + (value: any),
167+
);
168+
}
169+
143170
export function setValueForNamespacedAttribute(
144171
node: Element,
145172
namespace: string,

0 commit comments

Comments
 (0)