Interacting with the DOM is one of the slowest operations in JavaScript. I'd suggest selecting all your elements before the loop, then referring to the cached elements inside.
On the same token, do not perform any DOM manipulation in a loop if you can help it. Since it doesn't look like you can, another approach is to detach all DOM elements you're going to rearrange before the loop, perform the manipulation inside the loop, then re-attach them to the DOM at the end.
A reverse while
whileloop would be faster than the forfor.If you're using a version of jQuery prior to 1.6, the .data()
.data()method carries a lot of overhead. In checkAgainstEl()checkAgainstEl(), use $.data($($item.select).get(val), 'personId')$.data($($item.select).get(val), 'personId')instead.It looks like you're re-wrapping jQuery objects in new jQuery objects unnecessarily, and in multiple places. Like in #4. I'm assuming variables prefixed with a $
$are jQuery objects; if they are, no need to wrap them in $()$()again.
Your biggest problem is the DOM interaction inside a loop. Cut that out any way you can and you'll be golden.