Created
November 25, 2017 02:24
-
-
Save steida/12b09fa7aad43d1de61dcd1406036229 to your computer and use it in GitHub Desktop.
Get React key for immutable object without memory leaks.
This file contains 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
export const getWeakKey = (() => { | |
const map = new WeakMap(); | |
let idx = 0; | |
return element => { | |
if (map.has(element)) return map.get(element); | |
idx++; | |
map.set(element, idx.toString()); | |
return idx; | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment