Improving Efficiency in jQuery function
<p>The while statement in this function runs too slow (prevents page load for 4-5 seconds) in IE/firefox, but fast in safari...</p>

<p>It's measuring pixel width of text on a page and truncating until text reaches ideal width:</p>

<pre><code>function constrain(text, ideal_width){

 $('.temp_item').html(text);
 var item_width = $('span.temp_item').width();
 var ideal = parseInt(ideal_width);
 var smaller_text = text;
 var original = text.length;

 while (item_width &gt; ideal) {
  smaller_text = smaller_text.substr(0, (smaller_text.length-1));
  $('.temp_item').html(smaller_text);
  item_width = $('span.temp_item').width();
 }

 var final_length = smaller_text.length;
 if (final_length != original) {
  return (smaller_text + '&amp;hellip;');
 } else {
    return text;
 }
}
</code></pre>

<p>Any way to improve performance? How would I convert this to a bubble-sort function?</p>

<p>Thanks!</p>
