Last active
December 17, 2015 10:18
-
-
Save cyx/5593415 to your computer and use it in GitHub Desktop.
Experimental thoughts on the ruby way of doing parameters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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