That ??™ s over 26 times longer! Therefore, in addition to the optimizations that can be made in
shrinking the overall file size of your application, you should also give priority to making performance
gains in execution based on your coding techniques. Here several best practices to consider.
Smart DOM Access
When working with client - side JavaScript, accessing the DOM can be at the heart of almost anything you
do. However, as essential as these DOM calls may be, it is important to remember that DOM access is
expensive from a performance standpoint and so should be done with forethought.
Cache DOM References
Cache references that you make to avoid multiple lookups on the same object or property. For example,
compare the following inefficient and efficient routines:
// Ineffecient
var str = document.createTextNode(???Farther up, further in???);
document.getElementById(???para1???).appendChild(str);
document.getElementById(???para1???).className=???special???;
// More efficient
Chapter 9: Bandwidth and Performance Optimizations
215
var str = document.createTextNode(???Farther up, further in???);
var p = document.getElementById(???para1???);
p.appendChild(str);
p.className=???special???;
What ??™ s more, if you make a sizeable number of references to a document or another common DOM
object, cache them, too.
Pages:
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249