Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby blocks vs JS callbacks
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);
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
You can’t perform that action at this time.