Last active
August 29, 2015 14:19
-
-
Save evilstreak/9d3cc767fd0734bbb1f0 to your computer and use it in GitHub Desktop.
Performance improvement from adding caching when rendering mustache partials for GOV.UK search results
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
$:.unshift "lib" | |
require "mustache" | |
require "benchmark/ips" | |
BASE_TEMPLATE = <<end_of_template | |
<h2>Names</h2> | |
{{#names}} | |
{{> user}} | |
{{/names}} | |
end_of_template | |
PARTIAL_TEMPLATE = ''' | |
<li{{#external}} class="external"{{/external}}> | |
<h3><a href="{{link}}" {{#external}}rel="external"{{/external}}>{{title}}</a></h3> | |
{{#debug_score}} | |
<p class="debug-link"> | |
{{link}} | |
</p> | |
<p class="debug-info"> | |
<span>Score: {{es_score}}</span> | |
<span>Format: {{#government}}government{{/government}} {{format}}</span> | |
</p> | |
{{/debug_score}} | |
{{#external}} | |
<p class="meta"> | |
<span class="visuallyhidden">Part of </span> | |
<span class="url">{{ display_link }}</span> | |
</p> | |
{{/external}} | |
{{#section}} | |
<p class="meta crumbtrail"> | |
<span class="visuallyhidden">Part of </span> | |
<span class="section">{{formatted_section_name}}</span> | |
{{#formatted_subsection_name}} | |
<span class="visuallyhidden">, </span> | |
<span class="subsection">{{ formatted_subsection_name }}</span> | |
{{/formatted_subsection_name}} | |
{{#formatted_subsubsection_name}} | |
<span class="visuallyhidden">, </span> | |
<span class="subsubsection">{{ formatted_subsubsection_name }}</span> | |
{{/formatted_subsubsection_name}} | |
</p> | |
{{/section}} | |
{{#metadata_any?}} | |
<ul class="attributes"> | |
{{#metadata}} | |
<li> {{{.}}} </li> | |
{{/metadata}} | |
</ul> | |
{{/metadata_any?}} | |
{{#historic?}} | |
{{#government_name}} | |
<p class="historic"> | |
First published during the {{government_name}} | |
</p> | |
{{/government_name}} | |
{{/historic?}} | |
<p>{{description}}</p> | |
{{#sections_present?}} | |
<ul class="sections"> | |
{{#sections}} | |
<li><a href="{{link}}#{{hash}}">{{title}}</a></li> | |
{{/sections}} | |
</ul> | |
{{/sections_present?}} | |
{{#examples_present?}} | |
<ul class="examples"> | |
{{#examples}} | |
<li> | |
<h4><a href="{{link}}">{{title}}</a></h4> | |
<p>{{description}}</p> | |
</li> | |
{{/examples}} | |
{{#suggested_filter_present?}} | |
<li> | |
<h4 class="see-all"><a href="{{suggested_filter_link}}">{{suggested_filter_title}}</a><h4> | |
</li> | |
{{/suggested_filter_present?}} | |
</ul> | |
{{/examples_present?}} | |
{{^examples_present?}} | |
{{#suggested_filter_present?}} | |
<h4 class="see-all"><a href="{{suggested_filter_link}}">{{suggested_filter_title}}</a><h4> | |
{{/suggested_filter_present?}} | |
{{/examples_present?}} | |
</li> | |
''' | |
one_name = [ | |
{name: "Charlie Chaplin"}, | |
] | |
data_10 = { | |
names: one_name * 10, | |
} | |
data_100 = { | |
names: one_name * 100, | |
} | |
data_1000 = { | |
names: one_name * 1000, | |
} | |
class Custom < Mustache | |
def partial(name) | |
PARTIAL_TEMPLATE | |
end | |
end | |
mustache = Custom.new | |
mustache.template = BASE_TEMPLATE | |
Benchmark.ips do |x| | |
x.time = 20 | |
x.report("render list of 10") do | |
mustache.render(data_10) | |
end | |
x.report("render list of 100") do | |
mustache.render(data_100) | |
end | |
x.report("render list of 1000") do | |
mustache.render(data_1000) | |
end | |
end |
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
Calculating ------------------------------------- | |
render list of 10 69.000 i/100ms | |
render list of 100 7.000 i/100ms | |
render list of 1000 1.000 i/100ms | |
------------------------------------------------- | |
render list of 10 706.772 (± 4.2%) i/s - 14.145k | |
render list of 100 70.872 (± 1.4%) i/s - 1.421k | |
render list of 1000 6.986 (± 0.0%) i/s - 140.000 |
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
Calculating ------------------------------------- | |
render list of 10 1.000 i/100ms | |
render list of 100 1.000 i/100ms | |
render list of 1000 1.000 i/100ms | |
------------------------------------------------- | |
render list of 10 11.939 (± 8.4%) i/s - 236.000 | |
render list of 100 1.220 (± 0.0%) i/s - 25.000 | |
render list of 1000 0.123 (± 0.0%) i/s - 3.000 in 24.305407s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment