Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memoize not working properly with objects and custom comparator #103

Closed
ohoho7 opened this issue Nov 27, 2020 · 1 comment
Closed

Memoize not working properly with objects and custom comparator #103

ohoho7 opened this issue Nov 27, 2020 · 1 comment

Comments

@ohoho7
Copy link

ohoho7 commented Nov 27, 2020

Comparing objects with custom comparator not working properly, example:

function nestedXtoString(myObj) {
    return myObj.x.toString();
}
const memoizedNestedXtoString = memoize(nestedXtoString, ([prev], [curr]) => {
    console.log("compared", prev, curr);
    return prev.x === curr.x;
});
const myObj = {x: 55};

console.log(memoizedNestedXtoString(myObj)); // "55"
myObj.x = 7;
console.log(myObj); // {x: 7}
console.log(memoizedNestedXtoString(myObj)); // 55

Comparator gives log:

compared {x: 7} {x: 7}
@Ayub-Begimkulov
Copy link

Ayub-Begimkulov commented Feb 7, 2021

@ohoho7 Everything is working as expected. Your comparator function will be called each time you call memoized function, no matter if the passed argument is referentially equal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants