While reworking the semantic web stuff I'm doing to be more interesting, I've noticed that while things are starting to work, is it very slow. Clicking on an actor's name takes almost two seconds before the actor's biography and list of movies they have been in appears.
Half of this slowdown is caused by the DOM appendChild function. So I traced through the code to see why it might be taking a while.
Here is what happens when appendChild is called for a XUL element:
- Check the node origin to make sure it can be appended.
- Ensure that the node is not an ancestor.
- If the node is already is the tree, remove the child first.
- Check to ensure that the existing children have been generated already, and build them if not. (Template content for instance)
- Append the content to the children array.
- Set the parent node.
- Inform the XBL system that the document for the elementis changing, if necessary.
- Set the document of the node.
- Check all attributes and if there are event attributes, hook up each event handler, compiling the script if needed.
- Repeat the last three steps recursively for all child nodes of the node being appended.
- Check if any DOM mutation listeners have been added to the window and would need to be notified of the appended node.
- If there are mutation listeners, fire a mutation event.
- Add the element's id to a hash, for later use of getElementById.
- If the element has a commandupdater attribute, hook it up.
- Check if the element is an observes element, and look up the corresponding broadcaster and hook the observer up to it.
- If the element isn't an observer, check for an observes attribute, and hook it up to the broadcaster.
- Check if the element is a keyset and hook up a key handler as needed.
- Check if the element has a datasources attribute, and if so load the datasource and handle all the template related stuff.
- Repeat steps 13-18 recursively for all child nodes.
- Check if the parent has an XBL binding.
- If so (most XUL elements will), get the insertion points, which may nested deeper in the tree.
- Look up the anonymous content for the element.
- Loop over the insertion points and find the appropriate one to use.
- Add the content at that point.
- Notify any document observers that the node had been appended.
Anyway, that might explain some of the delay, just in case you thought it was as simple as adding something to an array.