dom-expressions
dom-expressions copied to clipboard
Prefer to set property when property & attribute are identical
Changes
- It saves 5+ bytes for every single attribute that's instead set as a property, so I've added a list of reflected attributes to the Properties list.
- Fix 1 typo in a comment
Bundle size win
Example code
render(
() => (
<>
<a id={Math.random() > 0.5 ? 'linkA' : 'linkB'}>link</a> {/* id */}
<button name={Math.random() > 0.5 ? 'name a' : 'name b'}>name btn</button> {/* name */}
<link rel={Math.random() > 0.5 ? 'preload' : 'preconnect'}>link</link> {/* rel */}
<img src={logo} alt={Math.random() > 0.5 ? 'a img' : 'b img'} /> {/* src */}
<input type={Math.random() > 0.5 ? 'text' : 'password'} /> {/* type */}
</>
),
document.body
);
OLD output
(() => {
return [
((c = G()), e(() => a(c, 'id', Math.random() > 0.5 ? 'linkA' : 'linkB')), c),
((r = k()), e(() => a(r, 'name', Math.random() > 0.5 ? 'name a' : 'name b')), r),
((o = z()), e(() => a(o, 'rel', Math.random() > 0.5 ? 'preload' : 'preconnect')), o),
((n = E()),
a(n, 'src', 'data:image/svg+xml,somesvg'),
e(() => a(n, 'alt', Math.random() > 0.5 ? 'a img' : 'b img')),
n),
((t = B()), e(() => a(t, 'type', Math.random() > 0.5 ? 'text' : 'password')), t),
];
}, document.body);
NEW output (-0.1KB)
(() => {
return [
((c = A()), e(() => (c.id = Math.random() > 0.5 ? 'linkA' : 'linkB')), c),
((r = G()), e(() => (r.name = Math.random() > 0.5 ? 'name a' : 'name b')), r),
((o = k()), e(() => (o.rel = Math.random() > 0.5 ? 'preload' : 'preconnect')), o),
((n = O()),
(n.src = 'data:image/svg+xml,somesvg'),
e(() => (n.alt = Math.random() > 0.5 ? 'a img' : 'b img')),
n),
((t = E()), e(() => (t.type = Math.random() > 0.5 ? 'text' : 'password')), t),
];
var t, n, l, o, r, c, f;
}, document.body);
Notes
- There are more reflected properties to be added in the future for further gains, although some properties like
maxlength (string)
/maxLength (number)
will require slightly more effort to coerce types