Skip to content

Instantly share code, notes, and snippets.

@tarcieri
Created March 23, 2012 23:54
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 tarcieri/2176506 to your computer and use it in GitHub Desktop.
Save tarcieri/2176506 to your computer and use it in GitHub Desktop.
Roflscale your xml_http_request? calls by not casting them to booleans
#!/usr/bin/env ruby
require 'rubygems'
require 'benchmark/ips'
class ExampleRequest
def initialize
@env = {'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'}
end
def xml_http_request?
!(@env['HTTP_X_REQUESTED_WITH'] !~ /XMLHttpRequest/i)
end
def roflscale_xml_http_request?
@env['HTTP_X_REQUESTED_WITH'] =~ /XMLHttpRequest/i
end
end
request = ExampleRequest.new
Benchmark.ips do |ips|
ips.report("understandable") { request.xml_http_request? }
ips.report("roflscale") { request.roflscale_xml_http_request? }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment