Closed amaury1093 closed 5 years ago
Is that possible with the current version?
Yes it is, but you need to provide your own normalizer, which can work as e.g.:
const idCounter = 0;
const idMap = new WeakMap();
const getObjectId = obj => {
if (idMap.has(obj)) return iMap.get(obj);
const id = ++idCounter;
idMap.set(obj, id);
return id;
};
const memoized = memoize(f, {
normalizer: ([a, b]) => `${getObjectId(b)},${JSON.stringify(a)}`
});
Alternatively, if it would be the case where first argument is expected to be mapped by reference and second by serialized representation, it can be done more straightforward with weak
mode:
const memoizeWeak = require("memoizee/weak");
const memoized = memoizeWeak(f, { normalizer: ([,b]) => JSON.stringify(b) });
My function has signature:
If I understood well, when I memoize that, it memoizes by object reference:
I wish to memoize the 1st argument by its serialized version, and the 2nd argument by object reference. Namely, if:
The I want the 3 following tests to pass:
Test 1:
Test 2:
Test 3:
Is that possible with the current version?