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?
-
xpath has poor browser support or just isn't known at all. jQuery has made the whole CSS selector popular.Raynos– Raynos2011年11月02日 02:23:42 +00:00Commented 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.Demian Brecht– Demian Brecht2011年11月02日 02:26:58 +00:00Commented Nov 2, 2011 at 2:26
-
...And if there are any known reasons as to the limited support offered by browsers.Demian Brecht– Demian Brecht2011年11月02日 02:28:42 +00:00Commented Nov 2, 2011 at 2:28
1 Answer 1
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.
-
4Just 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.Michael Kay– Michael Kay2011年11月02日 08:19:10 +00:00Commented 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– Raynos2011年11月02日 11:42:05 +00:00Commented 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 ;)Demian Brecht– Demian Brecht2011年11月02日 18:49:18 +00:00Commented Nov 2, 2011 at 18:49