0

Is there a way to manually remove an object in javascript? Or is there only automatic garbage collection and it cannot be done? For example:

let s = new Set;
s.add( ... ); // let's say we've added in a million elements or so

Is there a way to then do delete s or something along those lines? I looked at Set but wasn't able to find something directly on that object.

David542
  • 104,438
  • 178
  • 489
  • 842
  • You can't delete an object in JS. When you drop all references to it, the GC will take care of cleaning it up. – FZs May 09 '22 at 16:01
  • "*Or is there only automatic garbage collection and it cannot be done?*" I'm not sure I understand. GC will run if the object has no reachable references to it. If you want to "delete" it, unset the variable. Or if the variable goes out of scope, that will also orphan the object. – VLAZ May 09 '22 at 16:01
  • An old but relevant question: [Can I trigger JavaScript's garbage collection?](https://stackoverflow.com/questions/8032928/can-i-trigger-javascripts-garbage-collection) – DBS May 09 '22 at 16:01

0 Answers0