views:

216

answers:

4

I am using JQuery to append large amounts of text inside a tag. I find the more text currently within the tag the slower appending is, and for large amounts of text it is too slow.

Is there a more efficient way to append text? Such as for instance creating dummy child tags and setting the content of them rather than appending to the parent?

A: 

Instead of appending multiple times, store each string in array and join the array when you append it to.

S.Mark
I want to give the user feedback about what is happening, so it is important for me to append content when available
Plumo
+1  A: 

Use DOM DocumentFragments to append a bunch a node, and take a look on Nicholas Zakas presentation in slideshare "writing-efficient-javascript"

wharsojo
+2  A: 

Check this presentation and this one too: jQuery Anti-Patterns
And in general:

  • do not .append() in loop
  • do not append directly into DOM
  • build a string and than append it or use DocumentFragment
  • do not append directly into DOM
NilColor
useful presentation
Plumo
+1  A: 

As i have been testing my site, i have noticed that .innerHTML is far quicker than jquery's append methods. This is depricated tho and support may dwindle and die in future browsers. That said the bgcolor attribute still works and i remember being told that was being depricated about a decade ago. Dont append scripts with the html fragment either as they need eval'ing (Jquery appears to do this automatically leading to slow times whereas with innerHTML you have to do it manually if needed). Eval is dead slow dont use it if possible. Again as previously mentioned joining an array is more efficeint than string concatenation. Use variables to point to dom nodes (ie. var this = document.getElementById("YourDomNode") then work with 'this' where needed instead of repeating the document.getElementById statement). Declaring variables outside of a function allows them to be accessible from any function. Please note this is client side script so the variable value is a unique reference for each user. Doing that highly repeated strings can reduce file sizes depending what your doing. If you absolutely have to load in your javascript inline as part of your html fragments try this:

Call this section after appending to the dom with innerHTML var f = function() {runScripts(document.getElementById('newDOM_Section'));}; setTimeout(f,1); ** Stick the following in your main .js file var stringScript var f function runScripts(element) { var scripts = element.getElementsByTagName("script"); var intScriptsCount = scripts.length for (var i = 0; i < intScriptsCount; i++) { stringScript = scripts[i].text setTimeout(stringScript, 1); }

}

The above code will make the dom changes visible before all the scripts are eval'd. This isnt multi-threading tho so be careful of its use. Far better to use attributes for event handlers (onclick, onchange etc.)

Finally before manipulating an element in the dom set the style to display:none, manipulate it then set the style back to display:inline (inline is default for some common elements) or wahtever it was previously.

This is just a quick mental dump of some tactics ive used so any input / derision of bad practice is welcome.

steve