In jQuery, to select the nth sibling of node, is .next () chained n times or .nextAll (': eq (n-1)') faster?

Assuming there is an arbitrary number of siblings from node, and I wanted to select nth sibling this node, should I use .next() chained n times, or should I just use one call .nextAll(':eq(n-1)') ?

It would seem that there would be a lot of extra overhead with the former for large n and possibly large overhead with the latter for a large number of siblings. I'm interested in a case that includes n = 2 and a large number of siblings, so I'm not sure if I want to use .next().next() or .nextAll(':eq(1)') . Does it matter?

Edit: for the case of n = 2 and many siblings, this looks like .next().next() faster than http://jsperf.com/next-next-vs-nextall-eq-1-vs-nextall- eq-1

+4
source share
2 answers

Here are some test cases for you to give you a general idea:

It is also very interesting to compare the following () vs getelementbyid:

You can take any of these test cases and add your own additional tests.

+3
source

What you need to do is visit http://jsperf.com . No need to worry or guess.

+4
source

All Articles