Re-using DOM objects vs. Creating new ones

Benchmark created on


Preparation HTML

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

Setup

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')";

Test runner

Ready to run.

Testing in
TestOps/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);
ready
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);
ready

Revisions

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