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?

3
  • xpath has poor browser support or just isn't known at all. jQuery has made the whole CSS selector popular.
    – Raynos
    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. Nov 2, 2011 at 2:26
  • ...And if there are any known reasons as to the limited support offered by browsers. 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.

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. Nov 2, 2011 at 8:19
  • @MichaelKay when you compare againts an implementation of the DOM, are you doing manual walking or using querySelector ?
    – Raynos
    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 ;) Nov 2, 2011 at 18:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.