DEV Community

Discussion on: Eradicating Memory Leaks In JavaScript

Collapse
 
qm3ster profile image
Mihail Malo • Edited
{
const {log} = console
const obj = { a: 1, b: 2, c: 3}
obj.a = null
obj.b = undefined
delete obj.c
log(
  'a:', obj.hasOwnProperty('a'),
  'b:', obj.hasOwnProperty('b'),
  'c:', obj.hasOwnProperty('c')
)
for (const k in obj) log(k)
log(Object.entries(obj))
}

delete is definitely slower, but it removes the key (completely cleans the object of the assignment).
This can matter if you are using an object as a hashmap with a large key domain, for example if the keys are UUIDs.
Over prolonged/intensive use, this can result in a large object.

But no reason to mess around with disposing of the object and using a new one, or using delete - just don't use objects for such cases, use the new Map type instead. The .delete(key) method there is very fast.