Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

82
Vistas
Difference between WeakSet<T> and Set<Weakref<T>>

I was recently wondering if there is any drawback for Set<Weakref<T>> over WeakSet<T>.

Pros:

  • The set is iterable

Cons:

  • The set must be filtered from no longer valid references

Am I missing something or is this almost a win-win situation? Would the Set<Weakref<T>> version prevent the garbage collector from collecting?

7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

What you missed:

WeakSets are collections of objects only. They cannot contain arbitrary values of any type, as Sets can.

The number of objects or their traversal order is immaterial, so a WeakSet is more suitable (and performant) than a Set for tracking object references, especially if a very large number of objects is involved.

MDN source

Set<Weakref<T>> will not prevent GC. Also note that WeakRef is not supported in Opera but WeakSet is.

7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos