refactor: Store memoized data on dom objects
Description
You know what I just remembered? Using WeakMap for memoization is entirely unnecessary; the fact that we're in a content script sandbox effectively is in itself a WeakMap.
I would choose to do or not do this entirely based on what one thinks makes the code more clear, which I could see arguments for both sides on (if anything I kind of lean toward weakMemoize, but I'd love to know your thoughts). I'm intentionally not going to discuss the performance implications, which are well below the threshold which I think it's appropriate to optimize.
Testing steps
Smoke test.
Presumably also works:
export const weakMemoize /* objectMemoize? */ = (func) => {
const key = getRandomHexString();
return (arg) => {
arg[key] ??= func(arg);
return arg[key];
};
};
One could even do a universal memoize, but as that would include typeof arg and a bunch of bullshit and memoization is generally for performance, that would be a bit silly.
Hm.
(In all seriousness—okay, no, not all seriousness, but still—an abandoned bug is still a bug, right? :3)
I wonder if this saves a property lookup.
This is a joke. If we start trying to save one property lookup so help me god.
export const timelineObject = postElement => {
return postElement.timelineObjectPromise ?? (postElement.timelineObjectPromise = inject('/main_world/unbury_timeline_object.js', [], postElement));
};
Hm.
That doesn't look like the JSdoc is parsing... at all? Needs @type or some other prefix.