Andrew Pallant

Software & Web Developer

Donate To Support My Blog Donate if this post helped you. Coffee money is always welcomed!

jQuery – Slow Selectors

Physical Link: jQuery – Slow Selectors

Recently I was experiencing slow jQuery functions. Slow jQuery was especially slow as I added more elements to the page and especially in Internet Explorer and FireFox. I was not surprised by the browsers being slower than others, but the fact that the jQuery was slower than it should was a surprise.

It turns out it was the selector that I had been using; $(“#ElementID:visible”) was slow very slow. In a function that did nothing much, it ended up taking 10 seconds to run.  I tried the selector $(“#ElementID”) and it ended up running is less than a second.  A developer that I had been working with thought maybe for some reason that it was the :visible filter that was applying, but I needed it.   I looked for another way to get the same result and found the .filter method.  It turns out that $(“ElementID”).filter(“:visible”) made my function run in less than a second.

In short:
Do Not Use:  $(“#ElementID:visible”) – Slow
Do Use: $(“ElementID”).filter(“:visible”) – Fast

Categories: Developement, jQuery, Web

©2024 LdnDeveloper