Skip to content

Instantly share code, notes, and snippets.

@cyx
Last active December 17, 2015 10:18
Show Gist options
  • Save cyx/5593415 to your computer and use it in GitHub Desktop.
Save cyx/5593415 to your computer and use it in GitHub Desktop.
Experimental thoughts on the ruby way of doing parameters.
require 'benchmark'
require 'rack'
require 'json'
payload = {
'user' => {
'a' => '1', 'b' => '2',
'comments' => [
{ 'body' => 'Hello' },
{ 'body' => 'World' }
]
}
}
qs = Rack::Utils.build_nested_query(payload)
json = JSON.dump(payload)
N = 1000
Benchmark.bmbm do |x|
x.report 'Rack::Utils.parse_nested_query' do
N.times do
Rack::Utils.parse_nested_query(qs)
end
end
x.report 'Rack::Utils.parse_query' do
N.times do
Rack::Utils.parse_query(qs)
end
end
x.report 'JSON.parse' do
N.times do
JSON.parse(json)
end
end
end
Rehearsal ------------------------------------------------------------------
Rack::Utils.parse_nested_query 0.080000 0.010000 0.090000 ( 0.077543)
Rack::Utils.parse_query 0.040000 0.000000 0.040000 ( 0.042824)
JSON.parse 0.020000 0.000000 0.020000 ( 0.012559)
--------------------------------------------------------- total: 0.150000sec
user system total real
Rack::Utils.parse_nested_query 0.070000 0.000000 0.070000 ( 0.076222)
Rack::Utils.parse_query 0.040000 0.000000 0.040000 ( 0.039440)
JSON.parse 0.010000 0.000000 0.010000 ( 0.005851)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment