title: Is there a cost associated with frequent object attribute access?
body: <p>One thing I mostly follow when writing code is if I see some object-attribute or method is being accessed multiple times within the same method, I tend to assign it to a variable and re-use it everywhere. </p>
  
  <pre class="lang-java prettyprint-override"><code>List&lt;ItemA&gt; itemAList = alpha.doX(..., ..., ...)
                .peek(item -&gt; packagedItems.put(item.getName(), ...))
                .peek(item -&gt; readyItems.put(item.getName(), ..., ...)))
                .map(item -&gt; transform(item.getName(), ...))
                .collect(toList());
  </code></pre>
  
  <p>As we can see <code>item.getName()</code> has been used multiple times. I would usually move it to a variable and re-use it:</p>
  
  <pre class="lang-java prettyprint-override"><code>String itemName = item.getName();
  List&lt;ItemA&gt; itemAList = alpha.doX(..., ..., ...)
                .peek(item -&gt; packagedItems.put(itemName, ...))
                .peek(item -&gt; readyItems.put(itemName, ..., ...)))
                .map(item -&gt; transform(itemName, ...))
                .collect(toList());
  </code></pre>
  
  <p>I would sometimes suggest the same in code-reviews as well. However, I would like to now if there are any performance cost with having to de-reference from the object instance over directly being able to access from a variable?  </p>
  
  <p>What are some of the thoughts around this practice? And in what circumstances <em>(if any)</em> one would be preferred over the other?</p>
  
  <p>Thank you.</p>