Created
August 25, 2014 01:28
-
-
Save sTiLL-iLL/68e687e921dab5b3ebb9 to your computer and use it in GitHub Desktop.
Fastest way to add new nodes to the DOM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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