replaceChild vs removeChild

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var A=[],l,q,w,g=document.createElement("DIV"),c;
    g.innerHTML='Em replaced!';
    function r(){
    A=[];
    c=document.createElement("DIV")
    for(var n=0;n!=200;n++){
    w=document.createElement("DIV");
    w.innerHTML=n;
    c.appendChild(w);
    A.push(w);
    }
    document.body.appendChild(c);
    l=A.length;
    }
    r();
};

Benchmark.prototype.teardown = function() {
    c.parentNode.removeChild(c)
    r();
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
removeChild
for(var n=0;n!=l;n++){
c.removeChild(A[n]);
}
pending…
replaceChild
for(var n=0;n!=l;n++){
c.replaceChild(g,A[n]);
}
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. Here’s a list of current revisions for this page:

1 comment

Add a comment