2

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]');
clarkk
  • 27,151
  • 72
  • 200
  • 340
  • Possible duplicate of https://stackoverflow.com/questions/1003855/howto-benchmark-javascript-code – CBroe Jul 31 '17 at 11:37
  • But before you start comparing which one of two ways to do the same thing is faster, you might want to make them actually do the same thing in the first place. `[data-id='+id+']:first` and `.children('[data-id='+id+']')` don’t select the same element(s). – CBroe Jul 31 '17 at 11:39
  • `.children('[data-id='+id+':first]')` ... now you’re looking for elements with `data-id="123:first"` or sth. like that ;-) – CBroe Jul 31 '17 at 11:42
  • Possible duplicate of https://stackoverflow.com/questions/17752874/multiple-selector-vs-single-selector-performance –  Jul 31 '17 at 11:45
  • You can use http://jsben.ch/ – aslantorret Jul 31 '17 at 11:46

1 Answers1

1

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.

gmo
  • 8,860
  • 3
  • 40
  • 51