thetenfold

DOMParser vs createElement-innerHTML

Jun 14th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Preparation code goes here
  2. function preparationCode() {
  3.  
  4.     // code here
  5.     // use global variable syntax because the other perf testing funcs need access
  6.     txt = "<html><head></head><body><div id='hi' class='box'>hello world</div><p>hey</p></body></html>";
  7.     regex = /^[\s\S]*<\s*body[^>]*>([\s\S]*)<\/body>[\s\S]*$/;
  8.  
  9. }
  10.  
  11.  
  12.  
  13.  
  14. // DEFINE FUNCTION 1 HERE
  15. function function1() {
  16.  
  17.     var p = new DOMParser(), div;
  18.     div = p.parseFromString(txt, "text/html");
  19.  
  20.     div.getElementById("hi").className;
  21.  
  22. }
  23.  
  24. // DEFINE FUNCTION 2 HERE
  25. function function2() {
  26.  
  27.     var text = txt.replace(regex, "$1"),
  28.     div = document.createElement("body"); div.innerHTML = text;
  29.    
  30.     document.evaluate(".//div[@id='hi']", div, null, 9, null).singleNodeValue.className;
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment