Skip to content

Commit

Permalink
Put common aliases in Map/Set instead of switch over strings (faceboo…
Browse files Browse the repository at this point in the history
…k#26551)

This is a follow up to facebook#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>
  • Loading branch information
2 people authored and AndyPengc12 committed Apr 15, 2024
1 parent 362b882 commit 02ccd96
Show file tree
Hide file tree
Showing 5 changed files with 492 additions and 1,447 deletions.
27 changes: 27 additions & 0 deletions packages/react-dom-bindings/src/client/DOMPropertyOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,33 @@ export function setValueForAttribute(
}
}

export function setValueForKnownAttribute(
node: Element,
name: string,
value: mixed,
) {
if (value === null) {
node.removeAttribute(name);
return;
}
switch (typeof value) {
case 'undefined':
case 'function':
case 'symbol':
case 'boolean': {
node.removeAttribute(name);
return;
}
}
if (__DEV__) {
checkAttributeStringCoercion(value, name);
}
node.setAttribute(
name,
enableTrustedTypesIntegration ? (value: any) : '' + (value: any),
);
}

export function setValueForNamespacedAttribute(
node: Element,
namespace: string,
Expand Down
Loading

0 comments on commit 02ccd96

Please sign in to comment.