loadxml vs jquery.parseXML (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Setup

function getLastElement(n)
    {
        var x = n.lastChild;
        while (x && x.nodeType != 1)
                x = x.previousSibling;
        return x;
    }
    function getFirstElement(n)
    {
        var x = n.firstChild;
        while (x && x.nodeType != 1)
                x = x.nextSibling;
        return x;
    }
    function getPrevElement(n)
    {
        var x = n.previousSibling;
        while(x && x.nodeType != 1)
                x = x.previousSibling;
        return x;
    }
    function getNextElement(n)
    {
        var x = n.nextSibling;
        while(x && x.nodeType != 1)
                x = x.nextSibling;
        return x;
    }
    function loadXml(data) {
                var self = this, xml, tmp;
                if (!data) {
                        return null;
                }
                try {
                        //Needed to parse Model to Xml (Serializer#objectToXml).
                        data = data.replace(/(\r\n)/g, '&#xA;');
                        data = data.replace(/(\r)/g, '&#xA;');
                        data = data.replace(/(\n)/g, '&#xA;');
                        
                        if (true) { //IE
                                xml = createDocument();
                                xml.loadXML(data);
                        } else { // Standard
                                tmp = new DOMParser();
                                xml = tmp.parseFromString(data, 'text/xml');
                        }
                } catch (e) {
                        xml = undefined;
                }
                
                if (!xml || !xml.documentElement || xml.getElementsByTagName('parsererror').length) {
                        throw 'Could not parse XML:\n' + data;
                }
                
                return getFirstElement(xml);
        }
        
    function unloadXml(xml) {
                var documentElement = xml && (xml.ownerDocument || xml).documentElement;
                if (documentElement && documentElement.firstChild) {
                        documentElement.removeChild(documentElement.firstChild);
                }
        }
        
    function createDocument() {
                var xmlDocument, tmp;
                if (false) {
                        xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
                        xmlDocument.async = false;
                        xmlDocument.validateOnParse = false;
                        xmlDocument.resolveExternals = false;
                        xmlDocument.preserveWhiteSpace = true; //AvD: used for getting .text for notes
                        xmlDocument.setProperty('SelectionLanguage', 'XPath');
                } else {
                        tmp = new DOMParser();
                        xmlDocument = tmp.parseFromString('<x/>', 'text/xml');
                }
                return xmlDocument;
        }
    function createElement(doc, tagName) {
                if (doc instanceof Element) {
                        doc = doc.ownerDocument; 
                }
                return doc.createElement(tagName);
        }
        
    function addChildNode(xml, tagname, attributes, value) {
                var child = xml.ownerDocument.createElement(tagname);
                xml.appendChild(child);
                if (value) {
                        var text = xml.ownerDocument.createTextNode(value);
                        child.appendChild(text);
                }
                if (attributes) {
                        for (var attributeName in attributes) {
                                if(attributes[attributeName]!=null) {
                                        child.setAttribute(attributeName, attributes[attributeName]);
                                }
                        }
                }
                return child;
        }
        
        function removeNodes(xpath, xml, variables) {
                var nodes = XPathCache.selectNodes(xpath, xml, variables)
                var toRemove = [];
                for (var i = 0; i < nodes.length; i++)
                        toRemove.push(nodes[i]);
                for (var i = 0; i < toRemove.length; i++)
                        toRemove[i].parentNode.removeChild(toRemove[i]);
        }

Test runner

Ready to run.

Testing in
TestOps/sec
loadXml
loadXml('<dummy />');
ready
jquery.parseXML
$.parseXML('<dummy />').children[0];
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.