forked from jekyll/jekyll
-
Notifications
You must be signed in to change notification settings - Fork 3
/
find-filter-vs-where-first-filters.rb
executable file
·49 lines (40 loc) · 1.33 KB
/
find-filter-vs-where-first-filters.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'benchmark/ips'
require_relative '../lib/jekyll'
puts ''
print 'Setting up... '
SITE = Jekyll::Site.new(
Jekyll.configuration({
"source" => File.expand_path("../docs", __dir__),
"destination" => File.expand_path("../docs/_site", __dir__),
"disable_disk_cache" => true,
"quiet" => true,
})
)
TEMPLATE_1 = Liquid::Template.parse(<<~HTML)
{%- assign doc = site.documents | where: 'url', '/docs/assets/' | first -%}
{{- doc.title -}}
HTML
TEMPLATE_2 = Liquid::Template.parse(<<~HTML)
{%- assign doc = site.documents | find: 'url', '/docs/assets/' -%}
{{- doc.title -}}
HTML
[:reset, :read, :generate].each { |phase| SITE.send(phase) }
puts 'done.'
puts 'Testing... '
puts " #{'where + first'.cyan} results in #{TEMPLATE_1.render(SITE.site_payload).inspect.green}"
puts " #{'find'.cyan} results in #{TEMPLATE_2.render(SITE.site_payload).inspect.green}"
if TEMPLATE_1.render(SITE.site_payload) == TEMPLATE_2.render(SITE.site_payload)
puts 'Success! Procceding to run benchmarks.'.green
puts ''
else
puts 'Something went wrong. Aborting.'.magenta
puts ''
return
end
Benchmark.ips do |x|
x.report('where + first') { TEMPLATE_1.render(SITE.site_payload) }
x.report('find') { TEMPLATE_2.render(SITE.site_payload) }
x.compare!
end