Skip to content

Commit

Permalink
Put common aliases in Map/Set instead of switch over strings (#26551)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
sebmarkbage and sophiebits authored Apr 4, 2023
1 parent d5fd60f commit c155796
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 c155796

Please sign in to comment.