/ ruby

Ruby and Rails performance tips

Ruby

  1. Pakai Symbol#to_proc
    credits https://github.com/rails/rails/pull/16833
  2. Use Rubocop and enable Performance cops
    rubocop will also recommend you to use Symbol#to_proc
    http://docs.rubocop.org/en/latest/cops_performance/
  3. use Array#- instead of Array#reject
    reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/array/access.rb
    need benchmark
  4. WIP

Rails

  1. Use Rails.logger.debug { block }
    sehingga Ruby tidak perlu menginstansiasi String object untuk menerjemahkannya, referensi: https://github.com/rails/rails/blob/48c287d1963fba09cf620527d35519f67d5ca01a/guides/source/debugging_rails_applications.md#impact-of-logs-on-performance
  2. use #stale? in controller
    Rails will return 302 HTTP code if not modified
  3. use ActiveRecord#update_all instead of ActiveRecord#update to update bunch of records
    ref https://github.com/rails/rails/blob/316513177cf9033d842cc176f8401d4e7c7e7c2a/activerecord/lib/active_record/persistence.rb
  4. WIP