File: template_vs_addressable.rb

package info (click to toggle)
ruby-mustermann 3.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,224 kB
  • sloc: ruby: 7,746; makefile: 6
file content (26 lines) | stat: -rw-r--r-- 1,010 bytes parent folder | download | duplicates (4)
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
$:.unshift File.expand_path('../lib', __dir__)

require 'benchmark'
require 'mustermann/template'
require 'addressable/template'

[Mustermann::Template, Addressable::Template].each do |klass|
  puts "", " #{klass} ".center(64, '=')
  Benchmark.bmbm do |x|
    no_capture = klass.new("/simple")
    x.report("no captures, match") { 1_000.times { no_capture.match('/simple') } }
    x.report("no captures, miss") { 1_000.times { no_capture.match('/miss') } }

    simple = klass.new("/{match}")
    x.report("simple, match") { 1_000.times { simple.match('/simple').captures } }
    x.report("simple, miss") { 1_000.times { simple.match('/mi/ss') } }

    explode = klass.new("{/segments*}")
    x.report("explode, match") { 1_000.times { explode.match("/a/b/c").captures } }
    x.report("explode, miss") { 1_000.times { explode.match("/a/b/c.miss") } }

    expand = klass.new("/prefix/{foo}/something/{bar}")
    x.report("expand") { 100.times { expand.expand(foo: 'foo', bar: 'bar').to_s } }
  end
  puts
end