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

WeakMap w/ frozen keys and garbage collection #134

Closed
lukescott opened this issue Nov 18, 2015 · 3 comments
Closed

WeakMap w/ frozen keys and garbage collection #134

lukescott opened this issue Nov 18, 2015 · 3 comments

Comments

@lukescott
Copy link

It looks like the current implementation uses a central store when a frozen key is used. This would mean that the keys would never be garbage collected, correct?

I was looking at another WeakMap implementation here: https://github.com/drses/weak-map

It overrides Object.freeze like this (as well as seal and preventExtensions):

var oldFreeze = Object.freeze;
Object.freeze = function(obj) {
    Object.defineProperty(obj, HIDDEN_KEY, {
        value: {}
    });
    return oldFreeze(obj);
};

This allows it to put a key-store on an object just before it is frozen to give it the opportunity to be used in a weak collection.

This approach may be a bit iffy. The drawbacks are 1) every frozen object gets an object added to it whether it's used or not, and 2) if an object is frozen before the freeze method is wrapped it won't work. But a huge pro to this approach is keys are garbage collected.

@zloirock
Copy link
Owner

Thanks for the proposal. Makes sense, perfect solution.

  1. It will not work for library version -- global Object.freeze-family methods will not be wrapped.

It's required some additional work for prevent side effects, but, I think, I will take it.

@zloirock
Copy link
Owner

  1. IE11 with WeakMap without support frozen keys and with separate leaking fix.

zloirock added a commit that referenced this issue Nov 23, 2015
@zloirock zloirock mentioned this issue Nov 24, 2015
20 tasks
@zloirock
Copy link
Owner

Available in 2.0.

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

No branches or pull requests

2 participants