replace-in-fragmentdocument-clone (v4)

Revision 4 of this benchmark created by 3po on


Setup

var f1, f2, doc = document;
    
    f1 = (function() {
    var f = doc.createDocumentFragment();
    f.appendChild(doc.createElement('div'));
    f.appendChild(doc.createElement('div'));
    f.appendChild(doc.createElement('div'));
    f.appendChild(doc.createElement('div'));
    var d = doc.createElement('div');
    d.appendChild(doc.createElement('div'));
    d.appendChild(doc.createElement('div'));
    d.appendChild(doc.createElement('div'));
    d.appendChild(doc.createElement('div'));
    var d2 = doc.createElement('div');
    d2.appendChild(doc.createElement('div'));
    d2.appendChild(doc.createElement('div'));
    var d3 = doc.createElement('div');
    d3.setAttribute('id', 'test');
    d2.appendChild(d3);
    d2.appendChild(doc.createElement('div'));
    d2.appendChild(doc.createElement('div'));
    d.appendChild(d2);
    d.appendChild(doc.createElement('div'));
    d.appendChild(doc.createElement('div'));
    d.appendChild(doc.createElement('div'));
    d.appendChild(doc.createElement('div'));
    f.appendChild(d);
    f.appendChild(doc.createElement('div'));
    f.appendChild(doc.createElement('div'));
    f.appendChild(doc.createElement('div'));
    f.appendChild(doc.createElement('div'));
    return f;
    })();
    
    f2 = (function() {
    var f = doc.createDocumentFragment();
    f.appendChild(doc.createElement('div'));
    f.appendChild(doc.createElement('div'));
    f.appendChild(doc.createElement('div'));
    f.appendChild(doc.createElement('div'));
    var d = doc.createElement('div');
    d.appendChild(doc.createElement('div'));
    d.appendChild(doc.createElement('div'));
    d.appendChild(doc.createElement('div'));
    d.appendChild(doc.createElement('div'));
    var d2 = doc.createElement('div');
    d2.appendChild(doc.createElement('div'));
    d2.appendChild(doc.createElement('div'));
    var d3 = document.createComment('');
    d2.appendChild(d3);
    d2.appendChild(doc.createElement('div'));
    d2.appendChild(doc.createElement('div'));
    d.appendChild(d2);
    d.appendChild(doc.createElement('div'));
    d.appendChild(doc.createElement('div'));
    d.appendChild(doc.createElement('div'));
    d.appendChild(doc.createElement('div'));
    f.appendChild(d);
    f.appendChild(doc.createElement('div'));
    f.appendChild(doc.createElement('div'));
    f.appendChild(doc.createElement('div'));
    f.appendChild(doc.createElement('div'));
    return f;
    })();
    
    function getAtPosWithNextSibling(el, idx) {
      var res = el.firstChild;
      for(var i = 0; i < idx; i++) {
        res = res.nextSibling;
      }
      return res;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
comment treewalker
var clone = f2.cloneNode(true);
var tw = document.createTreeWalker(clone, NodeFilter.SHOW_COMMENT, null, false);
var test = tw.nextNode();
var parent = test.parentNode;
parent.replaceChild(doc.createElement('span'), test);
test = null;
parent = null;
tw = null;
clone = null;
ready
childnodes
var clone = f1.cloneNode(true);
var parent = clone.childNodes[4].childNodes[4];
var test = parent.childNodes[2];
parent.replaceChild(doc.createElement('span'), test);
test = null;
parent = null;
clone = null;
ready
direct
(function(){var f = doc.createDocumentFragment();
f.appendChild(doc.createElement('div'));
f.appendChild(doc.createElement('div'));
f.appendChild(doc.createElement('div'));
f.appendChild(doc.createElement('div'));
var d = doc.createElement('div');
d.appendChild(doc.createElement('div'));
d.appendChild(doc.createElement('div'));
d.appendChild(doc.createElement('div'));
d.appendChild(doc.createElement('div'));
var d2 = doc.createElement('div');
d2.appendChild(doc.createElement('div'));
d2.appendChild(doc.createElement('div'));
var d3 = doc.createElement('span');
d2.appendChild(d3);
d2.appendChild(doc.createElement('div'));
d2.appendChild(doc.createElement('div'));
d.appendChild(d2);
d.appendChild(doc.createElement('div'));
d.appendChild(doc.createElement('div'));
d.appendChild(doc.createElement('div'));
d.appendChild(doc.createElement('div'));
f.appendChild(d);
f.appendChild(doc.createElement('div'));
f.appendChild(doc.createElement('div'));
f.appendChild(doc.createElement('div'));
f.appendChild(doc.createElement('div'));
})();
ready
nextSibling
var clone = f1.cloneNode(true);
var parent = getAtPosWithNextSibling(getAtPosWithNextSibling(clone,4),4);
var test = getAtPosWithNextSibling(parent,2);
parent.replaceChild(doc.createElement('span'), test);
test = null;
parent = null;
clone = null;
ready

Revisions

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