Removing an element from the screen

JavaScript performance comparison

Revision 3 of this test case created

Info

I observed that removeChild and display="none" were very slow when compared to just hiding an element. Here are some tests.

Preparation code

<script>
var testDIV = document.createElement('div');
testDIV.innerHTML = "Hello World";
</script>
<script>
Benchmark.prototype.setup = function() {
    document.body.appendChild(testDIV);
    testDIV.style.left = "10px";
    testDIV.style.top= "10px";
};

Benchmark.prototype.teardown = function() {
    document.body.appendChild(testDIV);
    testDIV.style.left = "10px";
    testDIV.style.top= "10px";
};
</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
Display = "none"
testDIV.style.display="block";

testDIV.style.display="none";

 
pending…
removeChild
document.body.appendChild(testDIV);

document.body.removeChild(testDIV);
 
pending…
Move off screen
testDIV.style.left="10px";

testDIV.style.left="-9999px";

 
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:

0 comments

Add a comment