I have an autocomplete form where the user can type in a term and it hides all <li>
elements that do not contain that term.
I originally looped through all <li>
with jQuery's each
and applied .hide()
to the ones that did not contain the term. This was WAY too slow.
I found that a faster way is to loop through all <li>
and apply class .hidden
to all that need to be hidden, and then at the end of the loop do $('.hidden').hide()
. This feels kind of hackish though.
A potentially faster way might be to rewrite the CSS rule for the .hidden
class using document.styleSheets
. Can anyone think of an even better way?
EDIT: Let me clarify something that I'm not sure too many people know about. If you alter the DOM in each iteration of a loop, and that alteration causes the page to be redrawn, that is going to be MUCH slower than "preparing" all your alterations and applying them all at once when the loop is finished.