Skip to content

Instantly share code, notes, and snippets.

@casperisfine
Created July 18, 2022 11:04
Show Gist options
  • Save casperisfine/7199579a138e268fda71d6a91366af49 to your computer and use it in GitHub Desktop.
Save casperisfine/7199579a138e268fda71d6a91366af49 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require 'bundler/inline'
gemfile(true) do
source "https://rubygems.org"
gem "rails", path: "/Users/byroot/src/github.com/Shopify/rails"
gem "benchmark-ips"
end
require 'rails'
require 'action_view'
require 'benchmark/ips'
class BenchView < ActionView::Base
def compiled_method_container
self.class
end
def self.compiled_method_container
self
end
end
view = BenchView.new(
ActionView::LookupContext,
{},
nil,
)
locals = { message: "Hello World!"}
erb_template = <<~ERB
<ul>
<% 10.times do %>
<li><%= message %>"</li>
<% end %>
</ul>
ERB
template = ActionView::Template.new(
erb_template,
"hello template",
ActionView::Template::Handlers::ERB.new,
virtual_path: "hello",
locals: locals.keys,
)
puts template.render(
view,
locals,
)
Benchmark.ips do |x|
x.report("ActionView::OutputBuffer") { template.render(view, locals, ActionView::OldOutputBuffer.new) }
x.report("optimized buffer") { template.render(view, locals, ActionView::FastOutputBuffer.new) }
x.compare!(order: :baseline)
end
Warming up --------------------------------------
ActionView::OutputBuffer
14.691k i/100ms
optimized buffer 22.712k i/100ms
Calculating -------------------------------------
ActionView::OutputBuffer
147.644k (± 1.4%) i/s - 749.241k in 5.075604s
optimized buffer 228.001k (± 1.7%) i/s - 1.158M in 5.081631s
Comparison:
ActionView::OutputBuffer: 147644.2 i/s
optimized buffer: 228001.4 i/s - 1.54x (± 0.00) faster
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment