tag:blogger.com,1999:blog-5103406499726689782.post4317628642830503237..comments2022-04-04T15:01:57.085+03:00Comments on fhtr: Optimizing a small JavaScript loopIlmari Heikkinenhttp://www.blogger.com/profile/10857385258792531336noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-5103406499726689782.post-14737651611884062042009-11-28T17:42:12.607+02:002009-11-28T17:42:12.607+02:00"Maybe property access is super expensive, le..."Maybe property access is super expensive, let's see if caching elem.offsetTop and elem.offsetHeight would help as well."<br /><br />Not all property access is slow, but things like scrollX/Y and offsetXXX forces browser to flush the possible pending layout changes.Consultanthttps://www.blogger.com/profile/01478583514296104856noreply@blogger.comtag:blogger.com,1999:blog-5103406499726689782.post-51422862524958798802009-11-15T06:08:45.901+02:002009-11-15T06:08:45.901+02:00Ilmari - Nicely done, it always feels good to swee...Ilmari - Nicely done, it always feels good to sweeten code. Just having a quick look at your final code a few things come to mind.<br /><br />First, you might consider moving the RegEx pattern instantiation outside the for loop.<br /><br />Second, you may also want to create those strings ('origsrc' & '/transparent.gif') outside the loop. Depending on how smart the JS engine is, it may not realize to make those constants. Btw, if you're only targeting FF only you could actually use the 'const' keyword instead of 'var'.<br /><br />Third, I think quite a few of the local variables you're using are unnecessary:<br /><br />'e' (in loop) - It should be more efficient to directly use imgs[i] everywhere.<br /><br />'ctop' - Why restore 'wsy' in this var? 'wsy' is already a local var.<br /><br />'cbottom' - You could keep this for readibility, but it's not necessary. Actually, same for 'top' and 'bottom'<br /><br />'etop' & 'ebottom' - For both, just just access the object property. There is no performance hit for accessing an object's property vs. a local var.Lonhttps://www.blogger.com/profile/02795923652468442563noreply@blogger.comtag:blogger.com,1999:blog-5103406499726689782.post-1790282680023798102009-11-14T22:32:49.450+02:002009-11-14T22:32:49.450+02:00You are my hero. And from the same country. Intere...You are my hero. And from the same country. Interesting blog to follow.Joonasnoreply@blogger.comtag:blogger.com,1999:blog-5103406499726689782.post-13576825292744627972009-11-14T18:25:25.953+02:002009-11-14T18:25:25.953+02:00Eli: See CRAZY MICRO-OPTIMIZATIONS above.Eli: See CRAZY MICRO-OPTIMIZATIONS above.Ilmari Heikkinenhttps://www.blogger.com/profile/10857385258792531336noreply@blogger.comtag:blogger.com,1999:blog-5103406499726689782.post-29657328349958169032009-11-14T16:17:03.523+02:002009-11-14T16:17:03.523+02:00Your "for (var i=0; i<imgs.length; i++)&qu...Your "for (var i=0; i<imgs.length; i++)" loop can be optimized to "var i = imgs.length; while (i--)".Elihttps://www.blogger.com/profile/09612865383954718128noreply@blogger.comtag:blogger.com,1999:blog-5103406499726689782.post-41726367344989873052009-11-14T14:51:00.348+02:002009-11-14T14:51:00.348+02:00Woah, incredible gains!Woah, incredible gains!Unknownhttps://www.blogger.com/profile/03014149400811353440noreply@blogger.com