clearing-child-elements

JavaScript performance comparison

Test case created

Info

Which is better: firstChild or indexing into children?

Preparation code

<div id="container">
</div>
<script>
Benchmark.prototype.setup = function() {
    var container = document.getElementById('container');
    for (var i = 0; i < 50; i++) {
      container.appendChild(document.createElement('div'));
    }
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
first child
while (container.firstChild) {
  container.removeChild(container.firstChild);
}
pending…
backtracking loop
var children = container.children;
var numberOfChildren = children.length;
for (var i = numberOfChildren - 1; i > -1; i--) {
  container.removeChild(children[i]);
}
pending…

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

Compare results of other browsers

0 comments

Add a comment