From: Eric Hodel Date: 2006-01-12T14:19:43+09:00 Subject: Re: Looking up properties and speed On Jan 11, 2006, at 5:58 PM, Chad Perrin wrote: > On Thu, Jan 12, 2006 at 10:03:30AM +0900, Eric Hodel wrote: >> On Jan 11, 2006, at 4:08 PM, Jonathan Leighton wrote: >> >>> 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). >> >> Why do you care? >> >> Why not make clean, readable code and optimize the parts that are >> causing the biggest slowdown? > > Hey, I'm curious about the answer too, whether I ever use that > knowledge > or not. What's wrong with curiosity? Nothing. But worrying about micro-optimizations will take all the fun away and give you little measurable benefit (by the 90/10 rule). If you write clean, readable code from the beginning and then go looking for the slow spots when you *need* to you'll be happiest. I promise. -- Eric Hodel - drbrain@segment7.net - http://segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com