I'd like to see a full example somewhere of how to handle using weak maps; it seems that one would pretty quickly get into a situation where something is GC'd but you still need it.
It's only GCed if no other references to the object exist. You can't dereference the object from the weakmap/weakset in any case, only from the other references.
Weakmaps and weaksets can be useful for storing arbitrary metadata about an object without modifying the object itself.
Whether a given element has been hovered can now be determined by checking previouslyHoveredEls.has(el), without polluting the elements themselves with additional properties. GC may be triggered by elements being removed from the DOM (if no other references to them remain), but won't be triggered otherwise.
You can't dereference the object from the weakmap/weakset in any case, only from the other references.
Can you expand on that? So delete wouldn't work, but there's no remove to undo an add? Is the main usecase having references from DOM nodes, and then deleting those nodes which in turn frees memory? So is this less useful for vdom oriented apps?
The DOM thing is one example use case. Another would be adding metadata to request/response objects in Node.js apps so that it can be read by middlewares later in the stack (without needing to modify any TypeScript interfaces).
Yes, there's a delete method for weak maps and weak sets. Like adding, it has zero effect on GC, which can't be manually controlled in JavaScript. Note that the delete operator used on an object also doesn't guarantee immediate GC.
I'd like to see a full example somewhere of how to handle using weak maps; it seems that one would pretty quickly get into a situation where something is GC'd but you still need it.
It's only GCed if no other references to the object exist. You can't dereference the object from the weakmap/weakset in any case, only from the other references.
Weakmaps and weaksets can be useful for storing arbitrary metadata about an object without modifying the object itself.
For example:
Whether a given element has been hovered can now be determined by checking
previouslyHoveredEls.has(el)
, without polluting the elements themselves with additional properties. GC may be triggered by elements being removed from the DOM (if no other references to them remain), but won't be triggered otherwise.Can you expand on that? So
delete
wouldn't work, but there's noremove
to undo anadd
? Is the main usecase having references from DOM nodes, and then deleting those nodes which in turn frees memory? So is this less useful for vdom oriented apps?The DOM thing is one example use case. Another would be adding metadata to request/response objects in Node.js apps so that it can be read by middlewares later in the stack (without needing to modify any TypeScript interfaces).
Yes, there's a
delete
method for weak maps and weak sets. Like adding, it has zero effect on GC, which can't be manually controlled in JavaScript. Note that thedelete
operator used on an object also doesn't guarantee immediate GC.I meant to say "eligible for gc".