However, you can speed up the routine considerably by moving the offending line to the end:
var comments=customBlog.getComments(???index??™);
var c=comments.count;
var entry;
var commentDiv = document.createElement(???div??™);
for (var i=0;i < c;i++) {
entry=document.createElement(???p??™);
entry.appendChild( document.createTextNode(comments[i]);
commentDiv.appendChild( entry );
}
document.body.appendChild(commentDiv);
With the restructured code, the document display only needs to be updated once instead of
multiple times.
Chapter 9: Bandwidth and Performance Optimizations
216
Combining document.write() calls
Along the same line, you should avoid excessive document.write() calls. Each call is a performance
hit. Therefore, a much better practice is to assemble a concatenated string variable first. For example,
compare the following:
// Inefficient
document.write(??? < div class=???row??? > ??™);
document.write(??? < label class=???cui??? > office < /label > ??™);
document.write(??? < a class=???cuiServiceLink??? target=???_self??? href=???tel:(765) 555-
1212??? > (765) 555-1212 < /a > ??™);
document.write(??? < /div > ??™);
// More efficient
var s = ??? < div class=???row??? > ??™ + ??? < label class=???cui??? > office < /label > ??™ +
??? < a class=???cuiServiceLink??? target=???_self??? href=???tel:(765) 555-1212??? > (765) 555-
1212 < /a > ??™ + ??? < /div > ??™;
document.
Pages:
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251