Skip to content

Instantly share code, notes, and snippets.

@kristoferjoseph
Forked from yoshuawuyts/memoizeNode.js
Created October 7, 2016 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristoferjoseph/97387665efc8c822f9e5039fadccee31 to your computer and use it in GitHub Desktop.
Save kristoferjoseph/97387665efc8c822f9e5039fadccee31 to your computer and use it in GitHub Desktop.
Helper function used in memoization of an HTML Node.
const elType = 'div'
module.exports = function memoizeNode (node) {
var placeholder = null
var element = null
var args = null
return function render () {
const _args = arguments
if (!element) {
args = _args
element = node
return element
} else {
const isEqual = compare(args, _args)
if (isEqual) {
placeholder = document.createElement(elType) // <template> has no IE support :O
placeholder.isSameNode = function (el) {
el.isSameNode(element)
}
return placeholder
} else {
args = _args
element = node
return element
}
}
}
}
// Compare two argument arrays
// ([any], [any]) -> bool
function compare (oldArgs, newArgs) {
var ln = newArgs.length
for (i = 0; i < ln; i++) {
if (newArgs[i] !== oldArgs[i]) {
return false
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment