简体   繁体   English

XML解析与DOM实现创建方法

[英]XML Parsing vs DOM Implementation create methods

I remember well that using the DOM implementation to create new HTML elements on a document was considered to be very much slower than assigning an HTML string to the 'innerHTML' property of the applicable HTML element. 我记得很清楚,使用DOM实现在文档上创建新的HTML元素被认为比将HTML字符串分配给适用的HTML元素的'innerHTML'属性要慢得多。

Does the same apply when authoring XML documents using JavaScript? 使用JavaScript编写XML文档时是否同样适用? Rather than using the DOM implementation's various create methods, would it be faster to just generate the XML string and parsing it? 与其使用DOM实现的各种创建方法,不如仅生成XML字符串并对其进行解析会更快吗?

Just something I wondered about.... :) 只是我想知道的事情... :)

*EDIT - Added an example * *编辑-添加了一个示例*

Which is faster? 哪个更快? (I'll be using jQuery's parseXML function to do the parsing example): (我将使用jQuery的parseXML函数来执行解析示例):

var myXdoc = $.parseXML("<person><name>Bob</name><relation>Uncle</relation>");

Or 要么

var myXdoc

if (window.ActiveXObject) {
    myXdoc = new ActiveXObject("Microsoft.XMLDOM");
    myXdoc.async = false;
}
else if (document.implementation && document.implementation.createDocument)
    myXdoc = document.implementation.createDocument("", "", null);

var p = myXdoc.documentElement.appendChild(myXdoc.createElement("person"));
var n = p.appendChild(myXdoc.createElement("name"));
n.appendChild(myXdoc.createTextNode("Bob"));
var r = p.appendChild(myXdoc.createElement("relation"));
r.appendChild(myXdoc.createTextNode("Uncle"));

The first thing we have to know why createDocument() might be slow. 首先我们要知道为什么createDocument()可能很慢。 The reason is that the DOM is alive and if you are modifying it, it triggers a re-validation of the DOM tree and probably a redraw of the site. 原因是DOM仍处于活动状态,如果您对其进行修改,则会触发DOM树的重新验证,并可能触发站点的重新绘制。 Every time. 每次。 But we could avoid this unnecessary re-validation and re-draw by using createDocumentFragment() . 但是我们可以使用createDocumentFragment()避免这种不必要的重新验证和重画。 The DocumentFragment isn't part of the DOM and so it wont trigger any events. DocumentFragment不是DOM的一部分,因此它不会触发任何事件。 So you can build your complete DOM part and in the last step append it to the DOM tree. 因此,您可以构建完整的DOM部分,并在最后一步将其附加到DOM树中。 I think it's the fastest way to create large DOM parts. 我认为这是创建大型DOM部件的最快方法。

UPDATE I tested it in Firefox 7 using Firebug. 更新我使用Firebug在Firefox 7中对其进行了测试。 The code: 编码:

console.time("a");
for(var i=0; i<1000; i++) {
$.parseXML("<person><name>Bob</name><relation>Uncle</relation></person>")
}
console.timeEnd("a");

console.time("b");
for(var i=0; i<1000; i++) {
var myXdoc
if (document.createDocumentFragment) {
    myXdoc = document.createDocumentFragment();
}
var p = myXdoc.appendChild(document.createElement("person"));
var n = p.appendChild(document.createElement("name"));
n.appendChild(document.createTextNode("Bob"));
var r = p.appendChild(document.createElement("relation"));
r.appendChild(document.createTextNode("Uncle"));
}
console.timeEnd("b");

The result: "a" about 140ms and "b" about 35ms. 结果:“ a”约为140毫秒,“ b”约为35毫秒。 So the string parse version is slower. 因此,字符串解析版本比较慢。

UPDATE2 It's very likely that the second variant is faster in any other browser, too. UPDATE2第二种变体很可能在其他任何浏览器中也更快。 Because the parse method has to build the DOM object too and it's very likely that it uses the same methods (eg: document.createElement ). 因为parse方法也必须构建DOM对象,并且很有可能使用相同的方法(例如: document.createElement )。 So the parse method can't be faster. 因此解析方法不能再快了。 But it's slower because it has first to parse the string. 但这比较慢,因为它必须首先解析字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM