Last active
December 28, 2015 06:09
-
-
Save spencerwi/7455436 to your computer and use it in GitHub Desktop.
Buffering DOM updates using DocumentFragments
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
/* Assumption: arrayOfElements is an array of DOM nodes created from a template or otherwise. */ | |
/* Don't do this! This reflows the DOM a *lot* */ | |
var targetNode = document.getElementById('target'); | |
var appendToTargetNode = targetNode.appendChild.bind(targetNode); | |
arrayOfElements.forEach(appendToTargetNode); | |
/* Instead, use a DocumentFragment */ | |
var targetNode = document.getElementById('target'); | |
var docFrag = document.createDocumentFragment(); | |
var appendToDocFrag = docFrag.appendChild.bind(docFrag); | |
arrayOfElements.forEach(appendtoDocFrag); | |
targetNode.appendChild(docFrag) /* 1 reflow */ | |
/* Total: 1 reflow */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment