Created
March 23, 2012 23:54
-
-
Save tarcieri/2176506 to your computer and use it in GitHub Desktop.
Roflscale your xml_http_request? calls by not casting them to booleans
This file contains 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
#!/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