Skip to content

Instantly share code, notes, and snippets.

@theojulienne
Created December 19, 2020 01:07
Show Gist options
  • Save theojulienne/e91fc338d180e1710e29c81a5d701fab to your computer and use it in GitHub Desktop.
Save theojulienne/e91fc338d180e1710e29c81a5d701fab to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: 'rails/rails'
gem "benchmark-ips"
gem "stackprof"
end
require "action_controller"
class FooController < ActionController::Base; end
result = Benchmark.ips do |x|
rs_simple = ActionDispatch::Routing::RouteSet.new
rs_simple.draw do
(1..1000).each do |i|
get "/foo/bar/thing_#{i}", to: "foo#bar#{i}"
end
get "/blah", to: 'foo#base'
end
x.report("without params") {
rs_simple.recognize_path("/blah")
rs_simple.recognize_path("/foo/bar/thing_99")
}
rs_params = ActionDispatch::Routing::RouteSet.new
rs_params.draw do
(1..1000).each do |i|
get "/:user/:repo/thing_#{i}", to: "foo#bar#{i}"
end
get "/:foo", to: 'foo#base'
end
x.report("params without constraints") {
rs_params.recognize_path("/blah")
rs_params.recognize_path("/foo/bar/thing_99")
}
rs_constraints = ActionDispatch::Routing::RouteSet.new
rs_constraints.draw do
(1..1000).each do |i|
get "/:user/:repo/thing_#{i}", to: "foo#bar#{i}", user: /[a-zA-Z0-9]+/, repo: /[a-zA-Z0-9]+/
end
get "/:foo", to: 'foo#base'
end
x.report("params with constraints") {
rs_constraints.recognize_path("/blah")
rs_constraints.recognize_path("/foo/bar/thing_99")
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment