0
foo.v = {
  rolo: 'a',
  cholo: 'b',
  yolo: 'c'
};

I have a long-running stateful process and I create new objects for certain types of requests. My question is, if I have an object like v in memory.

Is it more performant in terms of memory re-allocation to do:

delete foo.v.cholo;

or is it sometimes better to do:

foo.v.cholo = undefined

I read that inside the bowels of V8, that the delete operator mutates objects and prevents them from being as easily garbage collected or something like that. See:

https://github.com/google/google-api-nodejs-client/issues/375

I also would consider using a Map instead of an object, I tried comparing the delete operator to Map.prototype.delete, but the the delete operator was more performant here, maybe my test is not very good:

{
    const obj = {};

    const start = Date.now();

    for(let i =0; i < 1000000; i++){
        obj['foo'] = true;
        delete obj['foo'];
    }

    console.log('done after:', Date.now() - start);

}



{
    const m = new Map();

    const start = Date.now();

    for(let i =0; i < 1000000; i++){
        m.set('foo',true);
        m.delete('foo');
    }

    console.log('done after:', Date.now() - start);

}
  • You should measure that in your code base. Also note that there are [differences between deleting and setting to `undefined`](https://stackoverflow.com/questions/14967535/delete-a-x-vs-a-x-undefined). – str Jul 16 '18 at 08:50
  • 1
    Possible duplicate of [In Javascript what is more efficient, deleting an element or setting it to undefined explicitly](https://stackoverflow.com/questions/13982306/in-javascript-what-is-more-efficient-deleting-an-element-or-setting-it-to-undef) – Orelsanpls Jul 16 '18 at 09:06

1 Answers1

1

V8 developer here. foo.v.cholo = undefined is definitely more efficient than delete foo.v.cholo, because it doesn't change the object's shape (=number, attributes, and internal location of properties). Garbage collection is not affected. It doesn't matter whether you use undefined or null as the zap-out value.

General rule of thumb: avoid delete whenever possible.

This holds in particular when:

  • you have the same set of properties which are sometimes present and sometimes not
  • you care about performance when accessing (any of the) properties on the object in question

If, on the other hand, you have a large set of possible property names which come and go (and are relatively unlikely to reappear), e.g. in a "query string" => "response" cache, then it's probably better to use a Map instead of a plain object.

jmrk
  • 34,271
  • 7
  • 59
  • 74
  • Ok cool, so `Map.prototype.delete` is a lot more performant? The thing is I will need to remote the keys from the object not just the value to clear up all the memory so... –  Jul 16 '18 at 21:38
  • I updated the OP with a comparison of the delete operator and Map.prototype.delete, seems like the former is more performant in terms of speed but not sure about memory. –  Jul 16 '18 at 21:49
  • Using `Map.prototype.delete` is fine. -- From a memory-freeing point of view, removing the keys only matters if there are many of them. -- Your test is indeed too small to be useful; if you make it only slightly more complicated you'll see very different results. Also, the speed of the `delete` operation itself is only a small part of the issue; using `delete` has ramifications on the speed of other code dealing with the same objects. – jmrk Jul 17 '18 at 16:24