Skip to content

Comment on Ruby 2.1: Profiling Rubyparent

Comments

Rewritten in longer form, assuming I've made no mistakes, the two versions are equivalent to...

  if params[:user_id].present?
    if !@owner
      @owner = User.find_by_login(params[:user_id])
    end
  end
vs
  if !@owner
    if params[:user_id].present?
      @owner = User.find_by_login(params[:user_id])
    end
  end
Checking the member variable truthiness is presumably faster than a dictionary lookup followed by a call to "present?". As @owner is probably frequently/always true after the first call (it will be unless find_by_login returns false/nil/? or another method resets @owner, given the same instance), this leads to fewer dictionary lookups and present? calls, resulting in a performance increase.

EDIT: Well that was an amusing spam of simultaneous answers.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.