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

Skip falsey attribute rendering only if it's boolean attribute #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

eagletmt
Copy link
Owner

This is an optimization idea of Hamlit.

@eagletmt
Copy link
Owner Author

It seems there's no standard on how to render true/false/nil of non-boolean attributes.

  • Haml, Slim, Faml (before this PR)
    • %span{disabled: true, hidden: false, readonly: nil, foo: true, bar: false, baz: nil} renders <span disabled foo></span> .
    • All falsey attributes are not rendered.
    • It doesn't consider boolean attributes or not.
  • Rails
    • tag("span", disabled: true, hidden: false, readonly: nil, foo: true, bar: false, baz: nil) renders <span disabled="disabled" foo="true" bar="false" /> .
    • Falsey attributes are not rendered if it is a boolean attribute.
    • nil attribute is not rendered if it isn't boolean attribute.
  • Faml (after this PR), Hamlt (maybe)
    • %span{disabled: true, hidden: false, readonly: nil, foo: true, bar: false, baz: nil} renders <span bar='false' baz='' disabled foo='true'></span> .
    • Falsey attributes are not rendered if it is a boolean attribute.
    • All attributes are rendered if it isn't a boolean attribute.

@eagletmt
Copy link
Owner Author

This optimization only contributes to the performacne of "dynamic attributes" by removing runtime conditional branching.

Input

- url = 'https://wanko.cc'
%a{href: url}

Before

_buf = []; extend ::Faml::Helpers; url = 'https://wanko.cc'; 
; _buf << ("<a".freeze); _faml_html1 = (url); case (_faml_html1); when true; _buf << (" href".freeze); when false, nil; else; _buf << (" href='".freeze); _buf << (::Temple::Utils.escape_html((_faml_html1))); _buf << ("'".freeze); end; _buf << ("></a>\n".freeze); 
; _buf = _buf.join

After

_buf = []; extend ::Faml::Helpers; url = 'https://wanko.cc'; 
; _buf << ("<a href='".freeze); _buf << (::Temple::Utils.escape_html((url))); _buf << ("'></a>\n".freeze); 
; _buf = _buf.join

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.

1 participant