Created
December 22, 2009 17:07
-
-
Save geekq/261862 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Check the impact of Nokogiri builder / HAML engine caching | |
require 'benchmark' | |
require 'sinatra-nokogiri-menu.rb' | |
require 'haml' | |
def render_with_haml(current = nil) | |
@engine ||= Haml::Engine.new <<here | |
%ul | |
- MENU_ITEMS.each do |item| | |
- opts = {} | |
- item, check_proc = *item if item.is_a?(Array) | |
- if check_proc.nil? or check_proc.call | |
- if current.to_s == item.to_s | |
%li{:class => 'current_item'} | |
= item.to_s | |
- else | |
%li= item.to_s | |
%li world | |
here | |
@engine.render(Object.new, :current => current) | |
end | |
# doc.li item.to_s, opts | |
puts render_with_haml(:contact) | |
puts | |
puts "Compare different approaches running 1000 times:\n" | |
Benchmark.bm(30) do |b| | |
b.report('Nokogiri, singleton builder') do | |
1000.times{ main_menu(:contact) } | |
end | |
b.report('Nokogiri, recreate builder') do | |
1000.times{ main_menu(:contact); @main_menu_builder = nil } | |
end | |
b.report('HAML, singleton engine') do | |
1000.times{ render_with_haml(:contact) } | |
end | |
b.report('HAML, recreate engine') do | |
1000.times{ render_with_haml(:contact); @engine = nil } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment