I have a div that is not contentEditable. I capture keystrokes, insert the associated char into a string in memory, and then call a render() function that replaces the innerHTML of the div with the current string.
My question is, why does this loop get slower and slower as the innerHTML get’s larger? All I’m doing is overwriting the innerHTML of the div with a straight string. Shouldn’t this be constant time?
dojo.byId('thisFrame').innerHTML = this.value.string;
I don’t understand how this is dependent on the size of the string at all. It slows down when the string’s length gets over about 200 characters, and slows down drastically from there on out.
is a DOM element. Setting the
innerHTMLproperty of a DOm element is not constant time because it causes a side effect which does not take constant time.Specifically, assigning to
myHTMLElement.innerHTMLcauses the browser to parse the string with its HTML parser and rewrite a chunk of the DOM.http://www.w3.org/TR/2008/WD-html5-20080610/dom.html#innerhtml0
Parsing HTML is at least linear in the amount of HTML, and replacing the DOM is at least linear in both the number of nodes removed and the number of nodes added.