JavaScript – XPath vs DOM API Comparison

domjavascriptxpath

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?

Best Answer

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.

Related Topic