Last active
August 29, 2015 13:55
-
-
Save kezabelle/8751828 to your computer and use it in GitHub Desktop.
Testing various ways to test a dictionary (say, a wsgi environ) for an AJAX request
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
stmt = """ | |
environ = { | |
'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest' | |
} | |
if 'HTTP_X_REQUESTED_WITH' in environ: | |
result = environ['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' | |
""" | |
timeit.timeit(stmt=stmt, number=10000000) | |
# yields 2.7133121490478516 |
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
stmt = """ | |
environ = { | |
'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest' | |
} | |
if 'HTTP_X_REQUESTED_WITH' in environ: | |
result = environ['HTTP_X_REQUESTED_WITH'][0] == 'X' | |
""" | |
timeit.timeit(stmt=stmt, number=10000000) | |
# yields 3.305811882019043 |
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
stmt = """ | |
environ = { | |
'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest' | |
} | |
if 'HTTP_X_REQUESTED_WITH' in environ: | |
result = len(environ['HTTP_X_REQUESTED_WITH']) == 14 | |
""" | |
timeit.timeit(stmt=stmt, number=10000000) | |
# yields 3.3983161449432373 |
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
stmt = """ | |
environ = { | |
'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest' | |
} | |
if 'HTTP_X_REQUESTED_WITH' in environ: | |
result = environ['HTTP_X_REQUESTED_WITH'].startswith('X') | |
""" | |
timeit.timeit(stmt=stmt, number=10000000) | |
# yields 5.772078037261963 |
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
stmt = """ | |
environ = { | |
'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest' | |
} | |
result = environ.get('HTTP_X_REQUESTED_WITH', '') == 'XMLHttpRequest' | |
""" | |
timeit.timeit(stmt=stmt, number=10000000) | |
# yields 3.8378548622131348 |
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
stmt = """ | |
environ = {} | |
result = environ.setdefault('HTTP_X_REQUESTED_WITH', '') == 'XMLHttpRequest' | |
""" | |
timeit.timeit(stmt=stmt, number=10000000) | |
# yields 3.446074962615967 |
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
stmt = """ | |
environ = {} | |
if 'HTTP_X_REQUESTED_WITH' in environ: | |
result = environ['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' | |
""" | |
timeit.timeit(stmt=stmt, number=10000000) | |
# yields 1.0769789218902588 |
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
stmt = """ | |
environ = { | |
'HTTP_X_REQUESTED_WITH': 'HttpRequest' | |
} | |
if 'HTTP_X_REQUESTED_WITH' in environ: | |
result = environ['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' | |
""" | |
timeit.timeit(stmt=stmt, number=10000000) | |
# yields 2.849605083465576 |
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
stmt = """ | |
environ = {} | |
result = 'HTTP_X_REQUESTED_WITH' in environ and environ['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' | |
""" | |
timeit.timeit(stmt=stmt, number=10000000) | |
# yields 1.4808499813079834 |
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
stmt = """ | |
environ = { | |
'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest' | |
} | |
result = 'HTTP_X_REQUESTED_WITH' in environ and environ['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' | |
""" | |
timeit.timeit(stmt=stmt, number=10000000) | |
# yields 2.8417441844940186 |
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
stmt = """ | |
environ = {} | |
try: | |
result = environ['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' | |
except KeyError: | |
result = False | |
""" | |
timeit.timeit(stmt=stmt, number=10000000) | |
# yields 15.797735214233398 |
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
stmt = """ | |
environ = { | |
'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest' | |
} | |
try: | |
result = environ['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' | |
except KeyError: | |
result = False | |
""" | |
timeit.timeit(stmt=stmt, number=10000000) | |
# yields 2.8694100379943848 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment