In jQuery, to select the nth sibling of a node, is .next() chained n times or .nextAll(':eq(n-1)') faster?
jquery, siblings
Solution
Here is a few test-cases for you to give you a general idea:
- http://jsperf.com/next-vs-nextall-frist/2
- http://jsperf.com/nextall-first-vs-nextuntil-last-next
Also very interesting is the comparing of next() vs getelementbyid:
- http://jsperf.com/next-vs-document-getelementbyid/2
You can take any of those test-cases and add your own additional tests.
Problem
Assuming there are an arbitrary number of siblings of a node, and I wanted to select the `nth sibling` of that node, should I be using `.next()` chained up n times, or should I just use a single call to `.nextAll(':eq(n-1)')`? Seems like there would be a lot of extra overhead with the former for large n, and possibly a bigger overhead with the latter for large number of siblings. I'm concerned with a case that involves 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, it looks like `.next().next()` is fastest according to http://jsperf.com/next-next-vs-nextall-eq-1-vs-nextall-eq-1