clarkk
clarkk

Reputation: 27689

what is fastest: single selector or multiple

Which one is fastest?

single selector

$('#main_body').find('[data-table="'+table+'"]:first > tbody > [data-id='+id+']:first');

multiple selectors

$('#main_body').find('[data-table="'+table+'"]:first').children('tbody').children('[data-id='+id+':first]');

Upvotes: 2

Views: 106

Answers (1)

gmo
gmo

Reputation: 9000

For a direct answer, I suggest using tools like jsperf and do some test.

Like: https://jsperf.com/what-is-fastest-single-selector-or-multiple.
Which give us:

single selector   : 19,992 ±2.49% (ops/sec) fastest
multiple selectors: 16,035 ±3.91% (ops/sec) 21% slower

For a full answer and explanation, I presume the first will be always quicker because is a one function call agains three.

Upvotes: 1

Related Questions