Skip to content

Instantly share code, notes, and snippets.

@k0kubun

k0kubun/Gemfile Secret

Last active May 28, 2016 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k0kubun/3ea0c0dba78ac2e832c8fbe5a104d04b to your computer and use it in GitHub Desktop.
Save k0kubun/3ea0c0dba78ac2e832c8fbe5a104d04b to your computer and use it in GitHub Desktop.
Rails 4.2.6, erbのCGI.escapeHTMLあり (2.5 GHz Intel Core i7, SSD)
require 'action_view'
require 'action_view/base'
require 'benchmark/ips'
require 'slim'
require 'haml'
require 'hamlit'
require 'hamlit/rails_template'
require 'rails/all'
require 'haml/template'
require 'faml'
require 'faml/rails_handler'
class Context
def header
'Colors'
end
def item
[ { name: 'red', current: true, url: '#red' },
{ name: 'green', current: false, url: '#green' },
{ name: 'blue', current: false, url: '#blue' } ]
end
end
ActionView::OutputBuffer.class_eval do
def unwrapped_html_escape(s)
s = s.to_s
if s.html_safe?
s
else
CGI.escapeHTML(ActiveSupport::Multibyte::Unicode.tidy_bytes(s))
end
end
private
def html_escape_interpolated_argument(arg)
(!html_safe? || arg.html_safe?) ? arg : CGI.escapeHTML(arg.to_s)
end
end
class Bench
def initialize
@erb = File.read('view.erb')
@haml = File.read('view.haml')
@slim = File.read('view.slim')
end
def run
Benchmark.ips do |x|
x.report("erb") { erb_template.render(view, {}) }
x.report("slim") { slim_template.render(view, {}) }
x.report("haml") { haml_template.render(view, {}) }
x.report("hamlit") { hamlit_template.render(view, {}) }
x.report("faml") { faml_template.render(view, {}) }
x.compare!
end
end
private
def view
@view ||= Context.new
end
def erb_template
@original_template ||= ActionView::Template.new(
@erb, 'erb', ActionView::Template::Handlers::ERB, locals: []
)
end
def slim_template
@slim_template ||= ActionView::Template.new(
@slim, 'slim', Slim::RailsTemplate.new, locals: []
)
end
def haml_template
@haml_template ||= ActionView::Template.new(
@haml, 'haml', Haml::Plugin, locals: []
)
end
def faml_template
@faml_template ||= ActionView::Template.new(
@haml, 'faml', Faml::RailsHandler.new, locals: []
)
end
def hamlit_template
@hamlit_template ||= ActionView::Template.new(
@haml, 'hamlit', Hamlit::RailsTemplate.new, locals: []
)
end
end
Bench.new.run
source 'https://rubygems.org'
gem 'rails', '4.2.6'
gem 'benchmark-ips'
gem 'slim'
gem 'haml'
gem 'faml'
gem 'hamlit'
Warming up --------------------------------------
erb 6.698k i/100ms
slim 6.632k i/100ms
haml 1.636k i/100ms
hamlit 7.632k i/100ms
faml 6.703k i/100ms
Calculating -------------------------------------
erb 72.601k (± 6.6%) i/s - 361.692k in 5.002753s
slim 69.081k (± 8.2%) i/s - 344.864k in 5.025056s
haml 16.509k (± 5.2%) i/s - 83.436k in 5.068098s
hamlit 77.318k (± 7.3%) i/s - 389.232k in 5.060095s
faml 68.967k (± 7.5%) i/s - 348.556k in 5.081773s
Comparison:
hamlit: 77317.6 i/s
erb: 72601.2 i/s - same-ish: difference falls within error
slim: 69081.3 i/s - same-ish: difference falls within error
faml: 68966.5 i/s - same-ish: difference falls within error
haml: 16509.0 i/s - 4.68x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment