denoland / deno_ast

Source text parsing, lexing, and AST related functionality for Deno
https://crates.io/crates/deno_ast
MIT License
146 stars 45 forks source link

feat: optimize boolean attr handling #174

Closed marvinhagemeister closed 10 months ago

marvinhagemeister commented 10 months ago

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" : "");