Re-using DOM objects vs. Creating new ones

JavaScript performance comparison

Test case created by

Preparation code

<div id="container"></div>
      
<script>
Benchmark.prototype.setup = function() {
  el = document.getElementById("container");
  var el1 = el.appendChild(document.createElement("div"));
  el1.style.background = "url('http://www.google.se/images/srpr/logo3w.png')";
  var el2 = el.appendChild(document.createElement("div"));
  el2.style.background = "url('http://www.google.se/images/srpr/logo3w.png')";
  var el3 = el.appendChild(document.createElement("div"));
  el3.style.background = "url('http://www.google.se/images/srpr/logo3w.png')";
  var el4 = el.appendChild(document.createElement("div"));
  el4.style.background = "url('http://www.google.se/images/srpr/logo3w.png')";

};
</script>

Preparation code output

<div id="container"></div>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Re-using DOM elements
var elToMove = el.removeChild(el.lastChild);
elToMove.style.backgroundPosition = -160*Math.round(Math.random()*18) + "px 0";
el.insertBefore(elToMove,el.firstChild);
pending…
Creating new DOM elements
el.removeChild(el.lastChild);
var newEl = document.createElement("div");
newEl.style.background = "url('http://www.google.se/images/srpr/logo3w.png')";
newEl.style.backgroundPosition = -160*Math.round(Math.random()*18) + "px 0";
el.insertBefore(newEl,el.firstChild);
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.

0 Comments