Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript/jQuery - performance issue
    primarykey
    data
    text
    <p>I have some kind of webapp here, which of course incorporates JavaScript and jQuery. On load, a functions wraps a <code>span</code> around every letter of a text - about 150 letters. Then the user can select the letters and after a confirmation, a result is displayed. Everything works nice and smooth, only the last part really kills the performance.</p> <p>The results are saved in three arrays. After on click the functions fires which adds classes to the clicked elements (this is the confirmation).</p> <p>I do it like this (3 times, for each array):</p> <pre><code>$.each(myArr, function( i, v ){ $(v).addClass( "my-class" ); }); </code></pre> <p>It works this way, but because I manipulate the DOM heavily it kills the performance.</p> <p>I am on a MacBook with 2.26 GHz and 2 GB RAM and I am not able to run a simple tooltip or anything after the classes has been added. Especially if one array is really full this has a negative performance impact.</p> <p>I already tried to optimize the script. I cached every DOM object that is used more then once, but this is not possible in every case (I think...). I also used selectors like <code>.my-class</code> and <code>#my-id</code> instead of <code>span[class = my-class]</code> and <code>span[id = my-id]</code> to speed up everything. Only the last array part is bad.</p> <p>Is there any way to optimize this <code>$.each</code> part? Caching or somehting? Or maybe using another technique?</p> <p><em>I don't expect the script to be SUPER fast - but adding a simple tooltip after the results are shown should be possible.</em></p>
    singulars
    1. This table or related slice is empty.
    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.
    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