Skip to content

Instantly share code, notes, and snippets.

@jgaskins
Created September 5, 2024 20:01
Show Gist options
  • Save jgaskins/8ca04e23a99ae1e3499f388c450538f0 to your computer and use it in GitHub Desktop.
Save jgaskins/8ca04e23a99ae1e3499f388c450538f0 to your computer and use it in GitHub Desktop.
Optimized Phlex-like components with Armature
➜ armature git:(master) ✗ crystal run --release bench/code_component.cr
<div class="users"><h2>Fox Mulder message</h2><p id="user-Fox Mulder"><span class="title">they:</span><span class="message">nested hello Fox Mulder</span></p></div><div class="users"><h2>Fox Mulder message</h2><p id="user-Fox Mulder"><span class="title">they:</span><span class="message">nested hello Fox Mulder</span></p></div><div class="users"><h2>Fox Mulder message</h2><p id="user-Fox Mulder"><span class="title">they:</span><span class="message">nested hello Fox Mulder</span></p></div>
name 929.18k ( 1.08µs) (± 0.87%) 96.0B/op fastest
require "benchmark"
require "../src/component"
record NestedNameComponent < Armature::Component, name : String do
render do |io|
3.times do
div(class: "users") do
h2 do
HTML.escape @name, io
io << " message"
end
p(id: "user-#{@name}") do
span(class: "title") { "they:" }
span(class: "message") do
io << "nested hello "
HTML.escape @name, io
end
end
end
end
end
end
puts NestedNameComponent.new(name: "Fox Mulder")
buffer = IO::Memory.new(1 << 16)
Benchmark.ips do |x|
x.report "name" { NestedNameComponent.new(name: "Fox Mulder").to_s buffer.rewind }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment