4

I just came up on xpath recently while writing a greasemonkey script and immediately fell in love. I haven't done any benchmarking yet (coming soon), but according to a couple sources that I read, xpath is faster than using the DOM API. After checking out the internals of a couple of libraries (Swizzle and jQuery), I noticed that neither use xpath, but use the DOM API.

My question is: Is there something here that I'm not seeing as to why these libraries haven't changed to use xpath (or were first written using xpath rather than the DOM API), or is it just that xpath just hasn't quite caught on yet?

asked Nov 2, 2011 at 1:57
3
  • xpath has poor browser support or just isn't known at all. jQuery has made the whole CSS selector popular. Commented Nov 2, 2011 at 2:23
  • I hadn't tested it on all browsers and versions (and haven't looked at any support tables).. I figured if nothing else, it would be due to that, but wondered if anyone knew of any other reasons. Commented Nov 2, 2011 at 2:26
  • ...And if there are any known reasons as to the limited support offered by browsers. Commented Nov 2, 2011 at 2:28

1 Answer 1

7

Apples to Oranges.

'XPath ' is a language for querying.

'DOM API' is allows you to not only access the document, but also interact with it. Eg: Adding and deleting nodes.

As to brenchmarking, that would be slightly nonsensical. The first is a language specification, the second is an API. How do you benchmark a specification? If you benchmark an implementation of XPath you will get vastly different results even between implementations, yet alone trying to compare to other conventions.

answered Nov 2, 2011 at 4:58
3
  • 4
    Just to confirm this, we measured a 100-fold difference recently between Saxon XPath running against a Saxon tree, and JDOM XPath running against a JDOM tree. Saxon XPath against a JDOM tree was somewhere in the middle. Performance is all about products, not about specs. Commented Nov 2, 2011 at 8:19
  • @MichaelKay when you compare againts an implementation of the DOM, are you doing manual walking or using querySelector ? Commented Nov 2, 2011 at 11:42
  • Good point re: Apples to Oranges.. I had query tunnel vision. About benchmarking, I'm curious (slightly) to see the differences both between implementations (both DOM API and XPath) as well as how performance is for querying (only as you pointed out) between XPath and DOM API for each implementation. Would the data actually be useful? Probably not, but I'd scratch an itch ;) Commented Nov 2, 2011 at 18:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.