04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 20: Best Practices<br />

list.innerHTML = html;<br />

This code constructs an HTML string and then assigns it to list.innerHTML , which creates the<br />

appropriate DOM structure. Although there is always a small performance hit for string concatenation,<br />

this technique still performs faster than performing multiple DOM manipulations.<br />

The key to using innerHTML , as with other DOM operations, is to minimize the number of times it is<br />

called. For instance, the following code uses innerHTML too much for this operation:<br />

var list = document.getElementById(“myList”);<br />

for (var i=0; i < 10; i++) {<br />

list.innerHTML += “ < li > Item “ + i + “ < /li > ”;<br />

}<br />

//AVOID!!!<br />

The problem with this code is that innerHTML is called each time through the loop, which is incredibly<br />

inefficient. A call to innerHTML is, in fact, a live update and should be treated as such. It ’ s far faster to<br />

build up a string and call innerHTML once than it is to call innerHTML multiple times.<br />

Use Event Delegation<br />

Most web applications make extensive use of event handlers for user interaction. There is a direct<br />

relationship between the number of event handlers on a page and the speed with which the page<br />

responds to user interaction. To mitigate these penalties, it ’ s best to use event delegation whenever<br />

possible.<br />

Event delegation, as discussed in Chapter 12 , takes advantage of events that bubble. Any event that<br />

bubbles can be handled not just at the event target, but also at any of the target ’ s ancestors. Using this<br />

knowledge, it ’ s possible to attach event handlers at a high level that are responsible for handling events<br />

for multiple targets. Whenever possible, attach an event handler at the document level that can handle<br />

events for the entire page.<br />

Beware of NodeLists<br />

The pitfalls of NodeList objects have been discussed throughout this book, because they are a big<br />

performance sink for web applications. Keep in mind that any time you access a NodeList , whether it be a<br />

property or a method, you are performing a query on the document, and that querying is quite expensive.<br />

Minimizing the number of times you access a NodeList can greatly improve the performance of a script.<br />

Perhaps the most important area in which to optimize NodeList access is loops. Moving the length<br />

calculation into the initialization portion of a for loop was discussed previously. Now consider this example:<br />

var images = document.getElementsByTagName(“img”);<br />

for (var i=0, len=images.length; i < len; i++){<br />

//process<br />

}<br />

659

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!