The time spent on that code will vary greatly depending on whether ulF
is already part of the document or not. If it is part of the document,
the browser has to recalculate the layout of the page with every
addition. Different browsers will show big differences in performance.
If ulF is not yet attached to the document, the rendering engine is not
called at all, not until the end when the whole branch is attached and
then the layout is calculated just once. If the ulF element exists, you
may hide it (display:none), the renderer will also ignore it until you
set it back to visible. Unfortunately, this is not always possible
since sometimes you have calculations involving the position of the
elements and, if it is not attached to the document or hidden, it has no
position, but that doesn't seem to be the case here.
Also, the methods you are using of Element are really wrappers for DOM
methods, many of them of the same name and functionality. You are
gaining nothing by using Element here.
Christian Tiberg wrote:
> The following code is executed around 900 times in order to populate
> two drag&drop UL's.
> var ulF = new YAHOO.util.Element(ulFran);
> while (ulF.hasChildNodes()) ulF.removeChild(ulF.get('firstChild'));
> for (var i=0; i<lst.length; i++)
> if (!redanValdDD(redanValda, lst[i].Artikelnr))
> var li = document.createElement("LI");
> = ulFran + "_" + i;
> li.artikelnr = lst[i].Artikelnr;
> li.innerHTML = produktDDTag(lst[i]);
> new DDList(li.id
> The call to produktDDTag is not the culprit, according to the
> profiler. The majority of the time is spent in the DDList constructor,
> and in adding the elements to the UL.
> How can the same code be so hugely different in execution time? Am I
> doing something horrendously wrong, or is there a way to simply do it
> faster? The DDList constructor is copied from one of the YUI examples.
> Best regards,
> Christian Tiberg
> No virus found in this incoming message.
> Checked by AVG - http://www.avg.com
> Version: 8.0.138 / Virus Database: 270.6.6/1623 - Release Date: 20/08/2008 8:12