I've added jsperf test to see the speed difference for different approaches to get the first child (total 1000+ children)
given, notif = $('#foo')
jQuery ways:
$(":first-child", notif)
- 4,304 ops/sec - fastest
notif.children(":first")
- 653 ops/sec - 85% slower
notif.children()[0]
- 1,416 ops/sec - 67% slower
Native ways:
- JavaScript native'
ele.firstChild
- 4,934,323 ops/sec (all the above approaches are 100% slower compared to firstChild
)
- Native DOM ele from jQery:
notif[0].firstChild
- 4,913,658 ops/sec
So, first 3 jQuery approaches are not recommended, at least for first-child (I doubt that would be the case with many other too). If you have a jQuery object and need to get the first-child, then get the native DOM element from the jQuery object, using array reference [0]
(recommended) or .get(0)
and use the ele.firstChild
. This gives the same identical results as regular JavaScript usage.
all tests are done in Chrome Canary build v15.0.854.0