Skip to content

Commit e9ea857

Browse files
ryansolidClaude via Cursorcursoragent
committed
perf(web): keep the per-key event check off the regex engine
The server spread walk and the client's assignProp test every key of a spread for `onXxx`. A `/^on[A-Z]/` test there regressed the spread-static-tail renderToString benches by 5-7% against the `startsWith("on")` it replaced. Gate on `startsWith("on")` and compare one char code, so a key not starting with `on` costs what it did before. `ssrAttribute` takes strings first, ahead of the new function branch, and h tests a prop's type before its name. Behavior is unchanged. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent b772e77 commit e9ea857

4 files changed

Lines changed: 11 additions & 5 deletions

File tree

‎packages/h/src/hyperscript.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function materialize(args: any[]): ExpandableNode | ExpandableNode[] {
114114
Object.defineProperty(l, "class", { ...d[k], value });
115115
classes = [];
116116
}
117-
if (k !== "ref" && !/^on[A-Z]/.test(k) && typeof d[k].value === "function") {
117+
if (typeof d[k].value === "function" && k !== "ref" && !/^on[A-Z]/.test(k)) {
118118
dynamicProperty(l, k);
119119
dynamic = true;
120120
} else if (d[k].get) dynamic = true;

‎packages/web/src/client.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,10 +2580,13 @@ function assignProp(node, prop, value, prev, skipRef, nodeName) {
25802580
return value;
25812581
}
25822582

2583+
let c;
25832584
const hasNamespace = prop.indexOf(":") > -1;
25842585

2585-
// Only `on` + an uppercase letter is an event; a lowercase `onclick` is an attribute.
2586-
if (!hasNamespace && /^on[A-Z]/.test(prop)) {
2586+
// Only `on` + an uppercase letter is an event; a lowercase `onclick` is an
2587+
// attribute. No regex on this per-prop path: a key not starting with `on`
2588+
// pays one `startsWith`.
2589+
if (!hasNamespace && prop.startsWith("on") && (c = prop.charCodeAt(2)) > 64 && c < 91) {
25872590
const name = prop.slice(2).toLowerCase();
25882591
const delegate = DelegatedEvents.has(name);
25892592
if (!delegate && prev) {

‎packages/web/src/constants.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,11 @@ function isHttpNavigationTarget(target: string): boolean {
233233
}
234234

235235
// Only `on` + an uppercase letter (`onClick`) is an event handler. Lowercase
236-
// `on*` names (`onclick`) are plain attributes.
236+
// `on*` names (`onclick`) are plain attributes. No regex: this runs on every
237+
// key of a spread, and a key not starting with `on` pays one `startsWith`.
237238
function isEventName(name: string): boolean {
238-
return /^on[A-Z]/.test(name);
239+
let c;
240+
return name.startsWith("on") && (c = name.charCodeAt(2)) > 64 && c < 91;
239241
}
240242

241243
export {

‎packages/web/src/server.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4618,6 +4618,7 @@ export function ssrAttribute(key, value) {
46184618
// passes a binding-slot value through untouched, so the position it names
46194619
// is bound here (principles §9.2.3). Both are trusted here so this hot
46204620
// path stays a pure string concatenation.
4621+
if (typeof value === "string") return ` ${key}="${value}"`;
46214622
if (value == null || value === false) return "";
46224623
if (typeof value === "object") {
46234624
return isSlotValue(value)

0 commit comments

Comments
 (0)