-
-
Save kristoferjoseph/97387665efc8c822f9e5039fadccee31 to your computer and use it in GitHub Desktop.
Helper function used in memoization of an HTML Node.
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
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