XPath vs DOM的性能 [英] Performance of XPath vs DOM

查看:121
本文介绍了XPath vs DOM的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人会在不同的场景中启发XPath和DOM之间的综合性能比较?我已经阅读了一些问题,例如 xPath vs DOM API,哪一个具有更好的性能 XPath或querySelector?。他们都没有提到具体的案例。这里有一些我可以开始的。


  1. 不涉及迭代。 getElementById(foobar)vs // * [@ id ='foobar']。前者是否比后者快?如果后者被优化,怎么办? / html / body / div [@ id ='foo'] / div [@ id ='foobar']?

  2. 涉及迭代。 getElementByX然后遍历子节点,XPath生成快照,然后遍历快照项。

  3. 涉及轴。 getElementByX然后遍历下一个兄弟姐妹vs // following-sibling :: foobar。

  4. 不同的实现。不同的浏览器和库以不同的方式实现XPath和DOM。哪个浏览器的XPath的实现更好?

作为 xPath vs DOM API,哪一个具有更好的性能说,平均程序员可能会在执行复杂的任务时松动(例如涉及多个轴),而XPath被保证优化。因此,我的问题只关心可以通过两种方式进行的简单选择。



感谢任何评论。

解决方案

XPath和DOM都是规范,而不是实现。您不能提出有关规范性能的问题,仅针对具体实现。快速XPath引擎与慢速XPath引擎之间至少存在10对1的不同之处:可能针对不同的内容进行优化,例如有些花费大量时间优化查询,假设将执行多次,这对于单次执行可能是错误的。可以说的一件事是,XPath的性能更多地依赖于您正在使用的引擎,而DOM的性能更多地取决于应用程序员的能力,因为它是一个较低级别的接口。当然,所有的程序员都认为自己要比平均水平好得多...


Would anyone enlighten me some comprehensive performance comparison between XPath and DOM in different scenarios? I've read some questions in SO like xPath vs DOM API, which one has a better performance and XPath or querySelector?. None of them mentions specific cases. Here's somethings I could start with.

  1. No iteration involved. getElementById(foobar) vs //*[@id='foobar']. Is former constantly faster than latter? What if the latter is optimized, e.g. /html/body/div[@id='foo']/div[@id='foobar']?
  2. Iteration involved. getElementByX then traverse through child nodes vs XPath generate snapshot then traverse through snapshot items.
  3. Axis involved. getElementByX then traverse for next siblings vs //following-sibling::foobar.
  4. Different implementations. Different browsers and libraries implement XPath and DOM differently. Which browser's implementation of XPath is better?

As the answer in xPath vs DOM API, which one has a better performance says, average programmer may screw up when implementing complicated tasks (e.g. multiple axes involved) in DOM way while XPath is guaranteed optimized. Therefore, my question only cares about the simple selections that can be done in both ways.

Thanks for any comment.

解决方案

XPath and DOM are both specifications, not implementations. You can't ask questions about the performance of a spec, only about specific implementations. There's at least a ten-to-one difference between a fast XPath engine and a slow one: and they may be optimized for different things, e.g. some spend a lot of time optimizing a query on the assumption it will be executed multiple times, which might be the wrong thing to do for single-shot execution. The one thing one can say is that the performance of XPath depends more on the engine you are using, and the performance of DOM depends more on the competence of the application programmer, because it's a lower-level interface. Of course all programmers consider themselves to be much better than average...

这篇关于XPath vs DOM的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆