Created
October 9, 2017 21:33
-
-
Save vaz/7b6437967741fecdbf480a4963e26988 to your computer and use it in GitHub Desktop.
Ruby blocks vs JS callbacks
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
function benchmark(cb) { | |
const start_time = 12345; | |
const result = cb('some message'); | |
console.log("the return value of cb was: ", result); | |
const end_time = 12456; | |
return end_time - start_time | |
} | |
const time_taken = benchmark((message) => { | |
let s = 'asdf'; | |
for (let i = 0; i < 100000; i++) { | |
s[2] = 'z'; | |
} | |
console.log("the message:", message) | |
return "some result value"; | |
}) | |
console.log(time_taken); | |
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
def benchmark | |
start_time = Time.now | |
result = yield 'some message' | |
puts "the return value of the block: #{result}" | |
end_time = Time.now | |
end_time - start_time | |
end | |
long_string = 'apple'*10000000 | |
running_time = benchmark do |msg| | |
long_string.reverse | |
puts "the message: #{msg}" | |
"some result value" | |
end | |
puts "it took: #{running_time}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment