xml vs json

JavaScript performance comparison

Revision 3 of this test case created by Heavensrevenge

Preparation code

<script src="https://code.jquery.com/jquery-git2.min.js"></script>

<script>
// Changes XML to JSON
function xmlToJson(xml) {
  
  // Create the return object
  var obj = {};

  if (xml.nodeType == 1) { // element
    // do attributes
    if (xml.attributes.length > 0) {
    obj["@attributes"] = {};
      for (var j = 0; j < xml.attributes.length; j++) {
        var attribute = xml.attributes.item(j);
        obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
      }
    }
  } else if (xml.nodeType == 3) { // text
    obj = xml.nodeValue;
  }

  // do children
  if (xml.hasChildNodes()) {
    for(var i = 0; i < xml.childNodes.length; i++) {
      var item = xml.childNodes.item(i);
      var nodeName = item.nodeName;
      if (typeof(obj[nodeName]) == "undefined") {
        obj[nodeName] = xmlToJson(item);
      } else {
        if (typeof(obj[nodeName].length) == "undefined") {
          var old = obj[nodeName];
          obj[nodeName] = [];
          obj[nodeName].push(old);
        }
        obj[nodeName].push(xmlToJson(item));
      }
    }
  }
  return obj;
};
</script>

      
<script>
Benchmark.prototype.setup = function() {
  testxml = '<svg>';
  for (i = 0; i < 1000; i++) {
     testxml += "<polyline id='i" + i + "'></polyline>";
  }
  testxml += '</svg>';
  parser = new DOMParser();
  xmlDoc = parser.parseFromString(testxml, "text/xml");
  jsonDoc = xmlToJson(xmlDoc);

};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Traverse XML DOM
idString = '';
$(xmlDoc).children().children().each(function(i,p) { 
   idString += ' ' + p.getAttribute('id'); 
});
pending…
Traverse JSON
obj = jsonDoc.svg.polyline;
objlen = obj.length;
idString = '';
for (i = 0; i < objlen; i++) {
   idString += ' ' + obj[i].id;
}
pending…
Convert XML > JSON and Traverse JSON
jsonDoc = xmlToJson(xmlDoc);
obj = jsonDoc.svg.polyline;
objlen = obj.length;
idString = '';
for (i = 0; i < objlen; i++) {
   idString += ' ' + obj[i].id;
}
pending…
Traverse XML with different id lookup
idString = '';
$(xmlDoc).children().children().each(function(i,p) { 
   idString += ' ' + p.attributes.item(0).nodeValue; 
});
pending…
Traverse XML with jQuery attribute get
idString = '';
$(xmlDoc).children().children().each(function(i,p) { 
   idString += ' ' + $(p).prop('id');
});
pending…
Traverse XML with jQuery direct attribute get
idString = '';
$(xmlDoc).children().children().each(function(i,p) {
   idString += ' ' + this.id;
});
pending…
Traverse XML without jQuery
idString = '';
chld = xmlDoc.childNodes.item(0).childNodes;
objlen = chld.length;
for (i = 0; i < objlen; i++) {
   idString += ' ' + chld.item(i).getAttribute('id');
};
pending…

Compare results of other browsers

Revisions

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

0 Comments