XKit-Rewritten icon indicating copy to clipboard operation
XKit-Rewritten copied to clipboard

refactor: Store memoized data on dom objects

Open marcustyphoon opened this issue 2 months ago • 4 comments

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.

marcustyphoon avatar Oct 28 '25 00:10 marcustyphoon

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.

marcustyphoon avatar Oct 28 '25 00:10 marcustyphoon

Hm.

(In all seriousness—okay, no, not all seriousness, but still—an abandoned bug is still a bug, right? :3)

marcustyphoon avatar Oct 28 '25 19:10 marcustyphoon

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));
};

marcustyphoon avatar Oct 28 '25 20:10 marcustyphoon

Hm.

That doesn't look like the JSdoc is parsing... at all? Needs @type or some other prefix.

AprilSylph avatar Oct 28 '25 20:10 AprilSylph