Skip to content

Instantly share code, notes, and snippets.

@ParthBarot-BoTreeConsulting
Created October 29, 2014 06:17
Show Gist options
  • Save ParthBarot-BoTreeConsulting/57c7f73de6be2cb32ce1 to your computer and use it in GitHub Desktop.
Save ParthBarot-BoTreeConsulting/57c7f73de6be2cb32ce1 to your computer and use it in GitHub Desktop.
Fast Ruby - Barcelona ruby conference video notes
=================================
def slow(&block)
block.call
end
def slow
Proc.new.call
end
- The below one is 5 times faster then above, in execution
def fast
yield
end
=================================
(1..100).map{|i| i.to_s}
(1..100).map(&:to_s) #20 times faster - https://github.com/rails/rails/pull/16833
=================================
Hash#merge # Slow
Hash#merge! # Fast - 3 times faster then above
=================================
enum.each_with_object({}) do |e, h|
h.merge!(e => e)
end
- The below one is 2 times faster then above
enum.each_with_object({}) do |e, h|
h[e] = e
end
=================================
Hash#fetch
Hash#fetch with block as a second argument # Faster 2 times
=================================
String#gsub
String#sub # 50% faster, so if not needed we should not use gsub
=================================
String#gsub
String#tr # 5 times faster then gsub
=================================
Should not use exceptions for control flow, instead we can put conditions. that would be 10 times faster.
=================================
Array#each_with_index
while loop # 80% faster then above, but more lines of code then above
=================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment