Note that there are some explanatory texts on larger screens.

plurals
  1. POoptimize dom-heavy JS sorting
    primarykey
    data
    text
    <p>So I've built this mail-program-like HTML viewer (no real emails, just some messages from user to user). It basically behaves just like windows explorer, where you have the labels at the top and the files you can sort by date or alphabetically. Each message looks like this:</p> <pre><code>&lt;div class="mail_msg" d_id="363"&gt; &lt;div class="done"&gt;&lt;img src="../"&gt;&lt;/div&gt; &lt;div class="absender"&gt;demo&lt;/div&gt; &lt;div class="subject"&gt;subject&lt;/div&gt; &lt;div class="name"&gt;name&lt;/div&gt; &lt;div class="date"&gt;16.09.2010&lt;/div&gt; &lt;/div&gt; </code></pre> <p>Atop of all these messages there is a bar to sort them alphabetically or by date. (using prototypejs)</p> <pre><code>$$('.absender_label','.subject_label', '.bname_label').each(function(el){ el.observe('click', function(){ /* ... */ var tar = el.className.split('_')[0]; var els = $('widget_mail_'+target).select('.mail_msg'), sortedEls = els.sort(function(x, y) { var a = x.down('.'+tar).innerHTML.toLowerCase(), b = y.down('.'+tar).innerHTML.toLowerCase(); return a &lt; b ? -1 : a &gt; b ? 1 : 0; }); $('widget_mail_'+target).select('.mail_msg').invoke('remove'); sortedEls.each(function(x){ if(dir=='bottom') { $('widget_mail_'+target).insert({bottom:x}); } else if(dir=='top') { $('widget_mail_'+target).down('.mail_msg_label').insert({after:x}); } }); }); }); </code></pre> <p>I know that there is way too much DOM-manipulation going on. I select all related divs, sort them, throw away all the old unsorted messages and insert the sorted ones at the top or at the bottom (first click from A-Z, second click from Z-A).</p> <p>It still works with hundreds of messages, but it takes 2 or 3 seconds. I receive the messages as JSON and parse HTML from that, so using a table sorting script like <a href="http://yoast.com/articles/sortable-table/" rel="nofollow noreferrer">this</a> is not an option at this point.</p> <p>How can I optimize this sorting?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload