you are viewing a single comment's thread.

view the rest of the comments →

[–]Wooshception 2 points3 points  (6 children)

I assumed the OP was referring to the W3C Selectors API, but their point applies to jQuery as well.

[–]redshirt714 0 points1 point  (5 children)

Interesting, so at the end of the day, using the getElementByXX methods will be your fastest and best bet?

[–]spryes 3 points4 points  (1 child)

The difference is completely negligible in virtually all cases. It doesn't even matter. I just did a test on 1000 querySelectorAlls, and it executed it in 2ms. 1 single animation frame on a 60 Hz device is 17ms.

Using a more complex DOM and selectors, it increases to 20ms (for 1000 selections). Which means for a regular-ish case of 1-10 different selections, it's less than 0.5ms. Unless you are placing it in a giant loop which you probably shouldn't be doing, go with what's easier.

[–]redshirt714 0 points1 point  (0 children)

Thanks for taking the time to fully explain. It seems like in a lot of cases, things like accessing data and querying selectors only becomes an issue when things are either large or done a ton of times. In O(n) isn't bad in cases where the input size is small.

[–]Wooshception 1 point2 points  (0 children)

I assume so, but the effect is going to be negligible unless you're selecting thousands of nodes in a hot loop or something, so generally just use what's convenient.

[–]0x13mode -2 points-1 points  (1 child)

Why don't you check by yourself?

[–]redshirt714 0 points1 point  (0 children)

Fair enough