You can increase productivity by reusing existing node text.
A DIV element that contains only text is a DOM element that contains one child node, whose type is the text node. Text content is stored in this text node, and not inside its parent div element.
The main problem with methods like .text () in jQuery or .innerHTML () in the HTML DOM or even .innerText () and .textContent () is that they all create new node text (even if it already exists ) More specifically, all of the methods listed above begin by deleting all the child DOM nodes, including the existing text node (this is also done using the innerHTML, innerText, and textContent implementations). This leaves the context element (DIV, in your case) empty, without any children. Then they create a new node text type node, assigning it new text and adding it to the context element. innerHTML, in addition, tries to parse the input HTML string and build a DOM fragment from it.
The fastest way I know to replace existing node text is as follows:
var divs = $(".divs").get(), len = divs.length; while(--len) { divs[len].firstChild.data = "default"; }
This method does not create a new child element of the node, but simply replaces the text of the existing text node. Obviously, this method assumes node text already exists. To do this, your source document can be initialized with any non-empty text inside the DIV (and the exclusion of any other child elements).
Raoul
source share