{"id":668,"date":"2012-07-01T12:37:11","date_gmt":"2012-07-01T12:37:11","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=668"},"modified":"2012-07-01T12:37:11","modified_gmt":"2012-07-01T12:37:11","slug":"dom-lookups-fast","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/dom-lookups-fast\/","title":{"rendered":"DOM lookups are fast!"},"content":{"rendered":"<p>I was working on a basic reverse mortgage calculator (input monthly payment, it returns how much you can afford). Not being a math wiz, I didn&#39;t spend too much time figuring out how to solve an equation that involves multiple variables, as well as some that are repeated.<\/p>\n<p>Instead, I opted for a brute force approach, running through hundreds of calculations, tweaking the parameters a little bit each time, until an expected outcome is reached (or comes close). After completing my &quot;brute force loop&quot;, and running some tests, I was a little disappointed to find that only 400 calculations took 2.5 seconds to complete on Firefox 13.<\/p>\n<p>Then, it hit me, I was doing 3 DOM lookups with jQuery in each loop, i.e.<\/p>\n<p><code><br \/>\n\tfor(var i = 0; i &lt; 400; i++) {<br \/>\n\t&nbsp; \/\/ brute force stuff..<br \/>\n\t&nbsp; param = $(this).parent().parent().siblings(&#39;.myfield&#39;).val();<br \/>\n\t&nbsp; \/\/ more DOM lookups<\/p>\n<p>\t&nbsp; \/\/ formula that uses param<br \/>\n\t}<br \/>\n\t<\/code><\/p>\n<p>So, I was ecstatic, assuming that if I set a variable to the field values, outside of the for loop, I&#39;ll cut execution time dramatically.<\/p>\n<p><code>var param = $(this).parent().parent().siblings(&#39;.myfield&#39;).val();<br \/>\n\tfor(var i = 0; i &lt; 400; i++) {<br \/>\n\t&nbsp; \/\/ brute force stuff..<br \/>\n\t&nbsp; \/\/ formula that uses param<br \/>\n\t}<\/code><\/p>\n<p>I logged the time for both approaches, and much to my surprise, I only saved about 2 or 3% by dropping the DOM lookups.<\/p>\n<p>Just thought I let everyone in on that, in case you are looking for ways to optimize your code &#8211; start somewhere else!<\/p>\n<p>Oh, and as a little bonus, interesting to see how quickly different browsers run JS. My code was executed in the times listed below by browser:<\/p>\n<p>\n\tFF 13:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.5 seconds<br \/>\n\tIE&nbsp;&nbsp;&nbsp; 9:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 1.7 seconds<br \/>\n\tIE&nbsp;&nbsp;&nbsp; 8:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 5.1 seconds<br \/>\n\tIE&nbsp;&nbsp;&nbsp; 7:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4.8 seconds (what did they do wrong in IE 8!)<br \/>\n\tChrome 19:&nbsp; 1.5 seconds<br \/>\n\tSafari 5.1&nbsp;&nbsp;&nbsp;&nbsp; 1.8 seconds<\/p>\n<p>Nice going Google!<br \/>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was working on a basic reverse mortgage calculator (input monthly payment, it returns how much you can afford). Not being a math wiz, I&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[5],"tags":[157],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/668"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=668"}],"version-history":[{"count":1,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/668\/revisions"}],"predecessor-version":[{"id":669,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/668\/revisions\/669"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}