tmort / Socialite

Other
1.68k stars 164 forks source link

Suggestion: Use .querySelectorAll() over .getElementsByClassName() #1

Closed i-like-robots closed 12 years ago

i-like-robots commented 12 years ago

It may be useful to add an extra argument to the getElements method for a tag name. This would enable the use of the more performant .querySelectorAll().

E.G. context.querySelectorAll(tagName + '.' + className);

In my experience the fall back for this may also be faster.

JSPerf: http://jsperf.com/the-speed-of-selectors-with-tag-names/2

dbushell commented 12 years ago

Hi Matt, thanks great suggestion! I'll look into querySelectorAll. Ideally I don't want to specify a particular tag otherwise that would dictate how users must write their defaults (I suspect either a or div may be useful).

I'm planning to write a jQuery plugin version of Socialite where these things will be taken care of, but for the raw JS version I appreciate any tips to optimise the hell out of it!

dbushell commented 12 years ago

http://jsperf.com/getelementsbyclassname-vs-queryselectorall/5/

This test shows that getElementsByClassName is faster than querySelectorAll without a tagName.

i-like-robots commented 12 years ago

And then some, I have added this to my previous test http://jsperf.com/the-speed-of-selectors-with-tag-names/3

Interestingly I've thrown in a result from Opera which has a super-speedy querySelectorAll() implementation.

dbushell commented 12 years ago

Might be one to keep an eye, see how the other browsers catch up to Opera. That is fast!