From: Stefan Kaes Date: 2006-01-12T20:47:07+09:00 Subject: Re: Looking up properties and speed Jonathan Leighton wrote: > Hi, > > In Javascript, if one were to use the same property value twice or more, > one would store it in a local variable because looking up the property > slows things down. > > Is the situation the same in Ruby? Is there any speed difference between > these: > > def foo > val = obj.prop > puts val > puts val > end > > def boo > puts obj.prop > puts obj.prop > end > > I assume not because I think the reason it's like that with Javascript > is because of the DOM (yeah, that's not strictly Javascript). > > Cheers > > Accessing a property involves a function call, which is probably slower than caching the value in a local variable. Even more so if the computation of the property value is expensive. However, I would worry about the speed difference only when the coding path is an application hot spot. -- stefan -- For rails performance tuning, see: http://railsexpress.de/blog Subscription: http://railsexpress.de/blog/xml/rss20/feed.xml