Skip to content

Instantly share code, notes, and snippets.

@nilbus
Last active December 11, 2015 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilbus/4526647 to your computer and use it in GitHub Desktop.
Save nilbus/4526647 to your computer and use it in GitHub Desktop.
# Benchmarks for https://github.com/rails/rails/pull/8914
require 'bench_press'
extend BenchPress
def original
headers = "requiresActiveX=true"
new_header = "IE=Edge,chrome=1"
headers << "," << new_header
end
def fixed_with_split
headers = "requiresActiveY=true"
new_header = "IE=Edgy,chrome=1"
new_header.split(',').each do |component|
unless headers[component]
headers << "," << component
end
end
end
def fixed_without_split
headers = "requiresActiveZ=true"
new_header = ["IE=Iffy", "chrome=2"]
new_header.each do |component|
unless headers[component]
headers << "," << component
end
end
end
def fixed_without_loop
headers = "requiresActiveZ=true"
new_header = "IE=Edgy,chrome=1"
unless headers[new_header]
headers << "," << new_header
end
end
def fixed_using_index
headers = "requiresActiveZ=true"
new_header = "IE=Edgy,chrome=1"
unless headers.index(new_header)
headers << "," << new_header
end
end
reps 500000
measure("original") { original }
measure("fixed") { fixed_with_split }
measure("fixed_without_split") { fixed_without_split }
measure("fixed_without_loop") { fixed_without_loop }
measure("fixed_using_index") { fixed_using_index }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment