patbenatar / rbexy

A Ruby template language and component framework inspired by JSX and React
MIT License
39 stars 5 forks source link

Make compiled output more efficient #47

Closed patbenatar closed 3 years ago

patbenatar commented 3 years ago
patbenatar commented 3 years ago

Inspecting the compiled code generated by ERB:

Flat HTML

<h2>Hello partial</h2>
@output_buffer.safe_append='<h2>Hello partial</h2>
'.freeze;
@output_buffer.to_s

Nested HTML

<div>
  <h2>Hello partial</h2>
</div>
@output_buffer.safe_append='<div>
  <h2>Hello partial</h2>
</div>
'.freeze;
@output_buffer.to_s

Nested HTML with Ruby

<div>
  <h2>Hello <%= "code".upcase %></h2>
</div>
@output_buffer.safe_append='<div>
  <h2>Hello '.freeze;@output_buffer.append=( \"code\".upcase );@output_buffer.safe_append='</h2>
</div>
'.freeze;
@output_buffer.to_s

HTML with Ruby attribute value

<br class="<%= "foo" %>" />
@output_buffer.safe_append='<br class=\"'.freeze;@output_buffer.append=( \"foo\" );@output_buffer.safe_append='\" />
'.freeze;
@output_buffer.to_s
patbenatar commented 3 years ago

Result

When rendering HTML: ~70% decrease in memory usage When rendering custom component classes: ~80% decrease

it "handles a bunch of html"

Before: 30588 bytes (473 objects) After: 10009 bytes (137 objects)

it "handles custom components with html"

Before: 20030 bytes (257 objects) After: 3904 bytes (50 objects)