Created
December 31, 2020 03:47
-
-
Save MatheusRich/9df2c9ee347d3569ae7647c9589136fb to your computer and use it in GitHub Desktop.
Comparing ruby native's ERB, Eruby and Rails' tag builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile do | |
source 'http://rubygems.org' | |
gem 'benchable' | |
gem 'rails' | |
gem 'erubi' | |
end | |
require 'action_view' | |
# require 'eruby' | |
include ActionView::Helpers::TagHelper | |
include ERB::Util | |
def output_buffer=(s) | |
end | |
def output_buffer | |
end | |
def test_tag(cls) | |
tag.aside(class: cls) do | |
tag.section(class: cls) do | |
tag.article(class: cls) do | |
tag.div(class: cls) do | |
tag.p(class: cls) do | |
tag.span(class: cls) do | |
"body" | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
def test_erb(cls) | |
b = binding | |
erb = <<~ERB | |
<aside class=<%= cls %> > | |
<section class=<%= cls %> > | |
<article class=<%= cls %> > | |
<div class=<%= cls %> > | |
<p class=<%= cls %> > | |
"body" | |
</p> | |
</div> | |
</article> | |
</section> | |
</aside> | |
ERB | |
ERB.new(erb).result(b) | |
end | |
def test_eruby(cls) | |
erb = <<~ERB | |
<aside class=<%= cls %> > | |
<section class=<%= cls %> > | |
<article class=<%= cls %> > | |
<div class=<%= cls %> > | |
<p class=<%= cls %> > | |
"body" | |
</p> | |
</div> | |
</article> | |
</section> | |
</aside> | |
ERB | |
eval Erubi::Engine.new(erb).src | |
end | |
Benchable.bench(:ips) do | |
bench 'erb' do | |
test_erb("txt-bold txt-md mx-auto my-2") | |
end | |
bench 'eruby' do | |
test_eruby("txt-bold txt-md mx-auto my-2") | |
end | |
bench 'tag' do | |
test_tag("txt-bold txt-md mx-auto my-2") | |
end | |
end | |
Benchable.bench(:memory) do | |
bench 'erb' do | |
test_erb("txt-bold txt-md mx-auto my-2") | |
end | |
bench 'eruby' do | |
test_eruby("txt-bold txt-md mx-auto my-2") | |
end | |
bench 'tag' do | |
test_tag("txt-bold txt-md mx-auto my-2") | |
end | |
end | |
# Benchmark IPS: | |
# Warming up -------------------------------------- | |
# Erb 1.024k i/100ms | |
# Eruby 1.506k i/100ms | |
# Tag 1.808k i/100ms | |
# Calculating ------------------------------------- | |
# Erb 10.053k (± 4.0%) i/s - 200.704k in 20.004021s | |
# Eruby 14.835k (± 5.5%) i/s - 296.682k in 20.082587s | |
# Tag 17.940k (± 6.7%) i/s - 357.984k in 20.083031s | |
# Benchmark Memory: | |
# Calculating ------------------------------------- | |
# Erb 8.764k memsize ( 0.000 retained) | |
# 126.000 objects ( 0.000 retained) | |
# 47.000 strings ( 0.000 retained) | |
# Eruby 5.954k memsize ( 0.000 retained) | |
# 69.000 objects ( 0.000 retained) | |
# 27.000 strings ( 0.000 retained) | |
# Tag 10.296k memsize ( 40.000 retained) | |
# 131.000 objects ( 1.000 retained) | |
# 17.000 strings ( 0.000 retained) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment