It seems that the bulk of the performance hit actually comes from this:
$ (this) .next () hide () ;.
At first, I thought you might get a performance hit because of how jquery can handle additional text nodes created by spaces between your elements, so I tried:
this.nextSibling.nextSibling.style.display = 'none';
It really didn't help, so it seems that just setting the style on this set of elements is extremely slow. To get around this, you might consider setting a default style for what you expect from the most common case, and then only react to another case. For the example script you posted, this will result in:
CSS
.Icons { display: none; }
New JS:
$('.Row .Cell .Text').each(function (index, item) { if (this.scrollWidth > $(this).parent().width()) $(this).next().show(); });
Appendix: it turns out that adding a class to all of these elements is a little faster, so you can do this http://jsfiddle.net/XuhaT/1/ :
CSS
#vTable { width:800px; } .Icon { display: none; } .visible.Icon { display: block; }
JS:
$("#countOfItems").html($('#vTable .Row ').length); var startDate = new Date(); $('.Row .Cell .Text ').each(function (index, item) { if (this.scrollWidth > $(this).parent().width()) $(this).next().addClass('visible'); }); var endDate = new Date(); $("#claculationTime").html(endDate - startDate);
source share