Skip to content

Instantly share code, notes, and snippets.

@a-axton
Created May 21, 2014 17:22
Show Gist options
  • Select an option

  • Save a-axton/8a5a6cdb379655b47eff to your computer and use it in GitHub Desktop.

Select an option

Save a-axton/8a5a6cdb379655b47eff to your computer and use it in GitHub Desktop.
Document fragments
var table = document.getElementById('t');
var tr = table.querySelector('tr');
var th = document.createElement('th');
var clone;
var df = document.createDocumentFragment();
for (var i = 0; i < 100; i++) {
// Performance tip: clone a node so that you don't reuse createElement()
clone = th.cloneNode();
clone.appendChild(document.createTextNode('hello' + i));
// Performance tip: append to the document fragment
df.appendChild(clone);
}
// Performance tip: append only once in the real DOM
tr.appendChild(df);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment