2

What are the advantages / disadvantages of the two different selectors?

Should I use one over the other?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
gregorygtseng
  • 851
  • 4
  • 12

2 Answers2

2

I think it's primarily a matter of user preference.

To select the first child of all <p> elements, you'd do:

  • $("//p/*[1]") in Xpath
  • $$("p > *:first-child") in CSS

I prefer using Xpath, but YMMV.

Note that, internally, all CSS selectors are converted to Xpath. For example, the selector $$("#one") will be converted into $(".//*[id='one']").

tdesikan
  • 513
  • 1
  • 4
  • 11
  • I read somewhere that CSS selectors are sometimes faster, but I think that's on client side DOM manipulation. Are there any metrics regarding performance of selectors in the Moovweb SDK? – gregorygtseng May 31 '13 at 18:43
  • 1
    As Aaron says in this response, the difference in CSS selector and Xpath selector performance is negligible. In both cases, you can scope your search to ensure faster performance. – tdesikan Jun 07 '13 at 19:12
2

Just a few notes:

  • indexing starts from 1 in XPath, so it's //p/*[1]
  • the CSS selectors in Tritium allow you to prefix a selector with >, as in $$("> p > :first-child"); this will be converted into a scoped search (i.e., ./p/*[1])
  • because CSS selectors are (currently) dynamically converted into XPath, there's a slight performance hit compared to using straight XPath