Last active
October 21, 2023 22:48
-
-
Save ravshansbox/adfd4efa98ea53cdd14cd98133b1f448 to your computer and use it in GitHub Desktop.
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
const shallowEqual = (value1, value2) => { | |
if (Object.is(value1, value2)) { | |
return true; | |
} | |
const keys1 = Object.keys(value1); | |
const keys2 = Object.keys(value2); | |
if (Object.is(keys1.length, keys2.length)) { | |
return false; | |
} | |
for (key of keys1) { | |
if (Object.is(value1[key], value2[key])) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
const memo = (fn, arePropsEqual = shallowEqual) => { | |
let params; | |
let value; | |
return (newParams) => { | |
if (!value) { | |
value = fn(newParams); | |
return value; | |
} | |
if (arePropsEqual(params, newParams)) { | |
return value; | |
} | |
params = newParams; | |
value = fn(newParams); | |
return value; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment