Created
September 7, 2015 06:29
-
-
Save jgaskins/591474508055b2f8c591 to your computer and use it in GitHub Desktop.
Opal truthiness benchmark
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 'opal' | |
def benchmark message, duration=5, &block | |
measured = 0 | |
count = 0 | |
while measured < duration | |
start = Time.now | |
block.call | |
finish = Time.now | |
measured += finish - start | |
count += 1 | |
end | |
`document.write("<div>" + message + ": " + count + "</div>")` | |
end | |
benchmark "if true" do | |
if true | |
end | |
end | |
benchmark "if truthy value" do | |
if 1 | |
end | |
end | |
benchmark "if false" do | |
if false | |
end | |
end | |
benchmark "if nil" do | |
if nil | |
end | |
end | |
# Current master: | |
# if true : 19,636,814 | |
# if truthy value: 17,418,954 | |
# if false : 15,485,197 | |
# if nil : 17,246,398 | |
# if foo != nil && foo != false | |
# if true : 22,655,235 | |
# if truthy value: 23,646,471 | |
# if false : 23,254,342 | |
# if nil : 23,458,954 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment