Hacker News new | ask | show | jobs
by alexh 5418 days ago
I was under the impression that jQuery cached selectors, so the advice "Cache jQuery Objects", while smart for many reasons, would not actually affect performance much at all.

Was I wrong?

1 comments

It depends on the selector and the browser. Something like $(".myclass") is worth caching on IE7 because it requires a pass through the entire document; there's no `querySelectorAll` or `getElementsByClassName`.

I wouldn't start by caching selectors all over the place unless you profile and see that it's slow. Doing a lot of selecting in a `scroll` or `mousemove` handler would be a bad idea though: http://ejohn.org/blog/learning-from-twitter/