4 ms·
One reason memory management is poor in webapps is due to the lack of weak pointers. WeakMap goes part way, but it's backwards: it has weak keys, not weak value
by pixelcort 13y ago
One reason memory management is poor in webapps is due to the lack of weak pointers. WeakMap goes part way, but it's backwards: it has weak keys, not weak values.
In larger webapps, you often end up wanting an identity map for your model instances, so you don't instantiate multiple instances of the same record. At the same time, you don't want your identity map holding strong pointers to all those instances, preventing them from being GC'd if nothing else is using them.
Over time your identity map just fills up with unused stuff and your app is killed.
Finally, if there are low-memory warning APIs in the browser, I'm not yet aware of them.
- kevingadd 13y agoI've been pushing for proper, no-foolin' Weak References to be added to the ECMAScript spec for something like a year now. App developers who want them need to speak up, because most of the people who work on the spec don't even understand the concept of a weak reference or comprehend why devs want them. It's an uphill battle, but with enough support they will probably add them - they were actually considered for ES6 but abandoned.
- qu4z-2 13y ago> it has weak keys, not weak values. Wow. That does seem backwards. What's the use-case for that sort of thing?
- pixelcort 13y agoThe weak keys is nice as it gives you a place to put "hidden" properties that can only be accessed when combining an object and the WeakMap. When the object is GC'd, the hidden properties in the WeakMap can as well. But you can get close using a naming convention on the objects proper, say double underscore prexix or whatnot. What can't be easily approximated right now is weak values, for identity maps in particular.