18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Chapter 19<br />

Use array and object literals<br />

I mentioned doing this to decrease byte count, but you can also replace array and object definitions with<br />

literals to decrease execution time. Consider the following object definition:<br />

var oFruit = new Object;<br />

oFruit.color = “red”;<br />

oFruit.name = “apple”;<br />

As mentioned previously, the previous code can be rewritten using an object literal:<br />

var oFruit = { color: “red”, name: “apple” };<br />

Besides the decrease in byte count, the object literal is one statement whereas the extended syntax is<br />

three statements; one statement is always executed faster than three. The same effect can be achieved<br />

using array literals instead of the extended array syntax.<br />

Use the DOM sparingly<br />

One of the most time-intensive operations in <strong>JavaScript</strong> is DOM manipulation. Anytime you add,<br />

remove, or otherwise change the underlying DOM structure of a page, you are incurring a significant<br />

time penalty. This happens because every DOM manipulation alters the appearance of the page to a<br />

user, meaning that the entire page must be recalculated to ensure that the page is rendered properly.<br />

The way to get around this problem is to do as many DOM manipulations as possible with elements<br />

that aren’t already in the DOM document.<br />

Consider the following example that adds 10 items to a bulleted list:<br />

var oUL = document.getElementById(“ulItems”);<br />

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

var oLI = document.createElement(“li”);<br />

oUL.appendChild(oLI);<br />

oLI.appendChild(document.createTextNode(“Item “ + i));<br />

}<br />

Two problems arise with this code in terms of execution speed. The first is the oUL.appendChild() call<br />

at the middle of the loop. Each time through, this line executes and the entire page must be recalculated<br />

to allow for the item to be updated. The second problem is the following line that adds the text node to<br />

the list item, which also causes page recalculation. With these two problems, every trip through the loop<br />

causes two page recalculations for a total of 20.<br />

To fix this problem, the list items should not be added until after the text nodes have been assigned.<br />

Additionally, you can use a document fragment to hold all the created list items until it’s time to add<br />

them to the list:<br />

var oUL = document.getElementById(“ulItems”);<br />

var oFragment = document.createDocumentFragment();<br />

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

var oLI = document.createElement(“li”);<br />

oLI.appendChild(document.createTextNode(“Item “ + i));<br />

592

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

Saved successfully!

Ooh no, something went wrong!