Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimize boolean attr handling #174

Merged
merged 2 commits into from
Oct 26, 2023
Merged

Conversation

marvinhagemeister
Copy link
Contributor

@marvinhagemeister marvinhagemeister commented Oct 26, 2023

HTML has a finite set of boolean attributes whose presence is interpreted as true, regardless of the actual attribute value and when it's not present it's interpreted as false. Knowing that, we can bypass calling into jsxattr to serialize and encode the value ourselves. If the value is a boolean literal we can serialize or skip the attribute ourselves too.

It probably doesn't matter much for perf in the grand scheme of things, but I guess why not go all in.

// input
<input type="checkbox" checked required={foo} />

// before
const $$_tpl_1 = [
  '<input type="checkbox" ',
  ">"
];
const a = _jsxssr($$_tpl_1, jsxattr("checked", true), jsxattr("required", foo));

// after
const $$_tpl_1 = [
  '<input type="checkbox" checked ',
  ">"
];
const a = _jsxssr($$_tpl_1, foo ? "required" : "");

Copy link
Member

@dsherret dsherret left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@marvinhagemeister marvinhagemeister merged commit bbde33d into main Oct 26, 2023
@marvinhagemeister marvinhagemeister deleted the boolean_attr_opt branch October 26, 2023 23:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants