Last active
April 16, 2016 21:42
-
-
Save developit/9efb42f3ba1ac995089a to your computer and use it in GitHub Desktop.
dom-recycler.js
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
/** Typed DOM node factory with reclaimation */ | |
let recycler = { | |
collect(node) { | |
recycler.clean(node); | |
let name = node.nodeName, | |
list = recycler.nodes[name]; | |
if (list) list.push(node); | |
else recycler.nodes[name] = [node]; | |
}, | |
create(nodeName) { | |
let name = nodeName.toUpperCase(), | |
list = recycler.nodes[name]; | |
return list && list.pop() || document.createElement(name); | |
}, | |
clean(node) { | |
node.remove(); | |
let len = node.attributes && node.attributes.length; | |
if (len) for (let i=len; i--; ) { | |
node.removeAttribute(node.attributes[i].name); | |
} | |
}, | |
nodes: {} | |
}; |
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
// instead of these: | |
let div = document.createElement('div'); | |
div.remove(); | |
// use these: | |
let div = recycler.create('div'); | |
recycler.collect(div); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment