Skip to content

Instantly share code, notes, and snippets.

@sTiLL-iLL
Created August 25, 2014 01:28
Show Gist options
  • Save sTiLL-iLL/68e687e921dab5b3ebb9 to your computer and use it in GitHub Desktop.
Save sTiLL-iLL/68e687e921dab5b3ebb9 to your computer and use it in GitHub Desktop.
Fastest way to add new nodes to the DOM
var frag = document.createDocumentFragment();
ajaxResult.items.forEach(function(item) {
// Create the LI element
var li = document.createElement('li');
li.innerHTML = item.text;
// Do some normal node operations on the LI here,
// like add classes, modify attributes,
// add event listeners, add child nodes, etc.
// *Instead place the LI into the fragment*
frag.appendChild(li);
});
// Lastly, mass-inject all list items via the DocumentFragment
document.querySelector('ul').appendChild(frag);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment