最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Which is faster, XPath or Regexp? - Stack Overflow

programmeradmin5浏览0评论

I am making an add-on for firefox and it loads a html page using ajax (add-on has it's XUL panel).

Now at this point, i did not search for a ways of creating a document object and placing the ajax request contents into it and then using xPath to find what i need.
Instead i am loading the contents and parsing it as text with regular expresion.

But i got a question. Which would be better to use, xPath or regular expression? Which is faster to perform?

The HTML page would consist of hundreds of elements which contain same text, and what i basically want to do is count how many elements are there.

I want my add-on to work as fast as possible and i do not know the mechanics behind regexp or xPath, so i don't know which is more effective.

Hope i was clear. Thanks

I am making an add-on for firefox and it loads a html page using ajax (add-on has it's XUL panel).

Now at this point, i did not search for a ways of creating a document object and placing the ajax request contents into it and then using xPath to find what i need.
Instead i am loading the contents and parsing it as text with regular expresion.

But i got a question. Which would be better to use, xPath or regular expression? Which is faster to perform?

The HTML page would consist of hundreds of elements which contain same text, and what i basically want to do is count how many elements are there.

I want my add-on to work as fast as possible and i do not know the mechanics behind regexp or xPath, so i don't know which is more effective.

Hope i was clear. Thanks

Share Improve this question edited Jul 15, 2013 at 12:29 holographic-principle 19.7k10 gold badges48 silver badges62 bronze badges asked Aug 4, 2010 at 13:50 user1651105user1651105 1,8074 gold badges28 silver badges45 bronze badges 4
  • 6 Obligatory link: Do not use regex – Amarghosh Commented Aug 4, 2010 at 13:57
  • Neither is inherently faster than the other - it all depends on their implementations. – Jeff Yates Commented Aug 4, 2010 at 14:03
  • 1 Just wondering, why do some people consider this "not a real question"? Asking for what type of approach is best (or fastest) for a typical programming task seems to me like a genuine question to ask at SO (imo). – Abel Commented Aug 4, 2010 at 14:11
  • @Abel - when asking on performance you need to show what is the requirement - ie if one takes 100ms and on 101ms then it does not matter which is faster – mmmmmm Commented Jul 15, 2013 at 12:25
Add a ment  | 

1 Answer 1

Reset to default 19

Whenever you are dealing with XML, use XPath (or XSLT, XQuery, SAX, DOM or any other XML-aware method to go through your data). Do never use regular expressions for this task.

Why? XML processing is intricate and dealing with all its oddities, external/parsed/unparsed entities, DTD's, processing instructions, whitespace handling, collapsing, unicode normalization, CDATA sections etc makes it very hard to create a reliable regex-way of getting your data. Just consider that it has taken the industry years to learn how to best parse XML, should be enough reason not to try to do this by yourself.

Answering your q.: when it es to speed (which should not be your primary concern here), it highly depends on the implementation of either the XPath or Regex piler / processor. Sometimes, XPath will be faster (i.e., when using keys, if possible, or piled XSLT), other times, regexes will be faster (if you can use a prepiled regex and your query is easy). But regexes are never easy with HTML/XML simply because of the matching nested parentheses (tags) problem, which cannot be reliably solved with regexes alone.

If input is huge, regex will tend to be faster, unless the XPath implementation can do streaming processing (which I believe is not the method inside Firefox).

You wrote:

"which is more effective"*

the one that brings you quickest to a reliable and stable implementation that's paratively speedy. Use XPath. It's what's used inside Firefox and other browsers as well if you need your code to run from a browser.

发布评论

评论列表(0)

  1. 暂无评论