Skip to content

Instantly share code, notes, and snippets.

@jeffque
Last active May 21, 2025 17:18
Show Gist options
  • Save jeffque/144cca0483411339fdec801ae45fb14e to your computer and use it in GitHub Desktop.
Save jeffque/144cca0483411339fdec801ae45fb14e to your computer and use it in GitHub Desktop.
Trying to create a benchmark for https://github.com/jekyll/jekyll/pull/9776
#!/usr/bin/env ruby
require "liquid"
require "benchmark/ips"
require "jekyll"
puts "Ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
puts "Liquid #{Liquid::VERSION}"
module Testing
class PostUrlAlways < Liquid::Tag
include Jekyll::Filters::URLFilters
def initialize(tag_name, post, tokens)
super
@orig_post = post.strip
end
def render(context)
@context = context
liquid_solved_orig_post = Liquid::Template.parse(@orig_post).render(context)
return liquid_solved_orig_post
end
end
class PostUrlNever < Liquid::Tag
include Jekyll::Filters::URLFilters
def initialize(tag_name, post, tokens)
super
@orig_post = post.strip
end
def render(context)
@context = context
return @orig_post
end
end
class PostUrlConditional < Liquid::Tag
include Jekyll::Filters::URLFilters
def initialize(tag_name, post, tokens)
super
@orig_post = post.strip
end
def render(context)
@context = context
content = @orig_post
return content if content.nil? || content.empty?
return content unless content.include?("{%") || content.include?("{{")
liquid_solved_orig_post = Liquid::Template.parse(content).render(context)
return liquid_solved_orig_post
end
end
end
Liquid::Template.register_tag("post_urlish_always", Testing::PostUrlAlways)
Liquid::Template.register_tag("post_urlish_never", Testing::PostUrlNever)
Liquid::Template.register_tag("post_urlish_conditional", Testing::PostUrlConditional)
template1 = '{% post_urlish_always foo{{bar}} %}'
template2 = '{% post_urlish_always foo42 %}'
template3 = '{% post_urlish_never foo42 %}'
template4 = '{% post_urlish_conditional foo{{bar}} %}'
template5 = '{% post_urlish_conditional foo42 %}'
def render(template)
Liquid::Template.parse(template).render("bar" => "42")
end
puts render(template1)
puts render(template2)
puts render(template3)
puts render(template4)
puts render(template5)
Benchmark.ips do |x|
x.report('post_urlish_always, with liquid tag') { render(template1) }
x.report('post_urlish_always, no liquid tag') { render(template2) }
x.report('post_urlish_never') { render(template3) }
x.report('post_urlish_conditional, with liquid tag') { render(template4) }
x.report('post_urlish_conditional, no liquid tag') { render(template5) }
end
@jeffque
Copy link
Author

jeffque commented May 21, 2025

Results:

$ ruby post-url-liquid-expansion-poc.rb
Ruby 3.2.1-p31
Liquid 4.0.4
foo42
foo42
foo42
foo42
foo42
ruby 3.2.1 (2023-02-08 revision 31819e82c8) [arm64-darwin22]
Warming up --------------------------------------
post_urlish_always, with liquid tag
                        10.093k i/100ms
post_urlish_always, no liquid tag
                        14.897k i/100ms
   post_urlish_never    25.130k i/100ms
post_urlish_conditional, with liquid tag
                        11.434k i/100ms
post_urlish_conditional, no liquid tag
                        24.115k i/100ms
Calculating -------------------------------------
post_urlish_always, with liquid tag
                        117.130k (± 4.1%) i/s    (8.54 μs/i) -    585.394k in   5.007115s
post_urlish_always, no liquid tag
                        151.364k (± 2.7%) i/s    (6.61 μs/i) -    759.747k in   5.022965s
   post_urlish_never    243.685k (± 3.5%) i/s    (4.10 μs/i) -      1.231M in   5.059676s
post_urlish_conditional, with liquid tag
                        114.436k (± 3.4%) i/s    (8.74 μs/i) -    571.700k in   5.001618s
post_urlish_conditional, no liquid tag
                        242.989k (± 1.6%) i/s    (4.12 μs/i) -      1.230M in   5.062740s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment