title: Cost of accessing an object in Scala
body: <p>A bit of context:</p>
  
  <pre><code>case class Finger(length: Int)
  case class Arm(thumb: Finger)
  case class Body(rightArm: Arm, leftArm: Arm)
  case class Human(body: Body)
  
  def processHumanFinger(human: Human) = println(human.body.rightArm.thumb.length)
  
  def processFingerOnly(finger: Finger) = println(finger.length)
  
  val john = Human(Body(Arm(Finger(1)), Arm(Finger(3))))
  val johnFinger = john.body.rightArm.thumb.length
  </code></pre>
  
  <p>I was wondering what was the difference in memory and time costs between those two pieces of code :</p>
  
  <h3>Sample 1</h3>
  
  <pre><code>(1 to 1000000).foreach(_ =&gt; processFingerOnly(johnFinger))
  </code></pre>
  
  <p>and</p>
  
  <h3>Sample 2</h3>
  
  <pre><code>(1 to 1000000).foreach(_ =&gt; processHumanFinger(john))
  </code></pre>
  
  <p>My question is mainly about knowing if the cost of accessing the human object in depth a million times (sample 2) is more expensive (in time and/or memory) than accessing it in depth once when assigning the <code>val johnFinger</code> then passing only the <code>Finger</code> object to the function (sample 1). But I am interested in every differences that exists between these two pieces of code !</p>